Skip to content

Commit 03c06c7

Browse files
authored
Update documentation in README.md (#13)
* Update documentation in README.md
1 parent 56705b3 commit 03c06c7

File tree

17 files changed

+293
-69
lines changed

17 files changed

+293
-69
lines changed

.github/tf.png

25.7 KB
Loading

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ website/vendor
3333

3434
# Keep windows files with windows line endings
3535
*.winfile eol=crlf
36+
37+
/examples/full/

README.md

+84-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# Terraform Provider Terrakube
1+
<a href="https://terraform.io">
2+
<img src=".github/tf.png" alt="Terraform logo" title="Terraform" align="left" height="50" />
3+
</a>
24

3-
**THIS IS WORK IN PROGRESS AND IT IS NOT STABLE**
5+
# Terraform Provider for Terrakube
6+
7+
The Terrakube Terraform Provider allows managing resources within your Terrakube installation.
48

59
## Requirements
610

@@ -10,9 +14,84 @@
1014
## Building The Provider
1115

1216
1. Clone the repository
13-
1. Enter the repository directory
14-
1. Build the provider using the Go `install` command:
17+
2. Enter the repository directory
18+
3. Build the provider using the Go `install` command:
19+
20+
```shell
21+
go install .
22+
```
1523

24+
Generate Docs
1625
```shell
17-
go install
26+
go generate ./...
27+
```
28+
29+
## Usage Example
30+
31+
```hcl
32+
terraform {
33+
required_providers {
34+
terrakube = {
35+
source = "registry.terraform.io/alfespa17/terrakube"
36+
}
37+
}
38+
}
39+
40+
provider "terrakube" {
41+
endpoint = "http://terrakube-api.minikube.net"
42+
token = "(PERSONAL ACCESS TOKEN OR TEAM TOKEN)"
43+
}
44+
45+
data "terrakube_organization" "org" {
46+
name = "simple"
47+
}
48+
49+
data "terrakube_vcs" "vcs" {
50+
name = "sample_vcs"
51+
organization_id = data.terrakube_organization.org.id
52+
}
53+
54+
data "terrakube_ssh" "ssh" {
55+
name = "sample_ssh"
56+
organization_id = data.terrakube_organization.org.id
57+
}
58+
59+
resource "terrakube_team" "team" {
60+
name = "TERRAKUBE_SUPER_ADMIN"
61+
organization_id = data.terrakube_vcs.vcs.organization_id
62+
manage_workspace = false
63+
manage_module = false
64+
manage_provider = true
65+
manage_vcs = true
66+
manage_template = true
67+
}
68+
69+
resource "terrakube_module" "module1" {
70+
name = "module_public_connection"
71+
organization_id = data.terrakube_ssh.ssh.organization_id
72+
description = "module_public_connection"
73+
provider_name = "aws"
74+
source = "https://github.com/terraform-aws-modules/terraform-aws-vpc.git"
75+
}
76+
77+
resource "terrakube_module" "module2" {
78+
name = "module_vcs_connection"
79+
organization_id = data.terrakube_ssh.ssh.organization_id
80+
description = "module_vcs_connection"
81+
provider_name = "aws"
82+
source = "https://github.com/terraform-aws-modules/super_private.git"
83+
vcs_id = data.terrakube_vcs.vcs.id
84+
}
85+
86+
resource "terrakube_module" "module3" {
87+
name = "module_ssh_connection"
88+
organization_id = data.terrakube_ssh.ssh.organization_id
89+
description = "module_ssh_connection"
90+
provider_name = "aws"
91+
source = "https://github.com/terraform-aws-modules/super_private.git"
92+
ssh_id = data.terrakube_ssh.ssh.id
93+
}
1894
```
95+
96+
* [Terrakube Docs](https://docs.terrakube.io/).
97+
* [Terrakube API Docs](https://docs.terrakube.io/api/methods).

docs/data-sources/ssh.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "terrakube_ssh Data Source - terraform-provider-terrakube"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# terrakube_ssh (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) Ssh Name
21+
- `organization_id` (String) Terrakube organization id
22+
23+
### Read-Only
24+
25+
- `description` (String) Ssh description information
26+
- `id` (String) Ssh Id

docs/data-sources/vcs.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "terrakube_vcs Data Source - terraform-provider-terrakube"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# terrakube_vcs (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) Vcs Name
21+
- `organization_id` (String) Terrakube organization id
22+
23+
### Read-Only
24+
25+
- `description` (String) Vcs description information
26+
- `id` (String) Vcs Id

docs/index.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ terraform {
2222
}
2323
2424
provider "terrakube" {
25-
endpoint = "http://terrakube-api.minikube.net"
25+
endpoint = "http://terrakube-api.minikube.net"
26+
token = "12345"
27+
insecure_http_client = true
2628
}
2729
```
2830

@@ -33,3 +35,7 @@ provider "terrakube" {
3335

3436
- `endpoint` (String) Terrakube API Endpoint. Example: https://terrakube-api.minikube.net
3537
- `token` (String) Personal Access Token generated in Terrakube UI (https://docs.terrakube.io/user-guide/organizations/api-tokens)
38+
39+
### Optional
40+
41+
- `insecure_http_client` (Boolean) Disable https certificate validation

docs/resources/module.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "terrakube_module Resource - terraform-provider-terrakube"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# terrakube_module (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `description` (String) Module description
21+
- `name` (String) Module name
22+
- `organization_id` (String) Terrakube organization id
23+
- `provider_name` (String) Module provider name. Example: azurerm, google, aws, etc
24+
- `source` (String) Source (git using https or ssh protocol)
25+
26+
### Optional
27+
28+
- `ssh_id` (String) Ssh connection ID for private modules
29+
- `vcs_id` (String) VCS connection ID for private modules
30+
31+
### Read-Only
32+
33+
- `id` (String) Module Id

examples/data-sources/ssh_example/data-source.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ data "terrakube_organization" "org" {
33
}
44

55
data "terrakube_ssh" "ssh" {
6-
name = "sample"
6+
name = "sample"
77
organization_id = data.terrakube_organization.org.id
88
}

examples/data-sources/vcs_example/data-source.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ data "terrakube_organization" "org" {
33
}
44

55
data "terrakube_vcs" "vcs" {
6-
name = "sample"
6+
name = "sample"
77
organization_id = data.terrakube_organization.org.id
88
}

examples/provider/provider.tf

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ terraform {
77
}
88

99
provider "terrakube" {
10-
endpoint = "http://terrakube-api.minikube.net"
11-
token = "XXX"
10+
endpoint = "http://terrakube-api.minikube.net"
11+
token = "12345"
12+
insecure_http_client = true
1213
}

internal/provider/module_resource.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,18 @@ func (r *ModuleResource) Configure(ctx context.Context, req resource.ConfigureRe
101101
return
102102
}
103103

104-
customTransport := http.DefaultTransport.(*http.Transport).Clone()
105-
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
106-
client := http.Client{Transport: customTransport}
104+
if providerData.InsecureHttpClient {
105+
if custom, ok := http.DefaultTransport.(*http.Transport); ok {
106+
customTransport := custom.Clone()
107+
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
108+
r.client = &http.Client{Transport: customTransport}
109+
} else {
110+
r.client = &http.Client{}
111+
}
112+
} else {
113+
r.client = &http.Client{}
114+
}
107115

108-
r.client = &client
109116
r.endpoint = providerData.Endpoint
110117
r.token = providerData.Token
111118

@@ -139,7 +146,12 @@ func (r *ModuleResource) Create(ctx context.Context, req resource.CreateRequest,
139146
}
140147

141148
var out = new(bytes.Buffer)
142-
jsonapi.MarshalPayload(out, bodyRequest)
149+
err := jsonapi.MarshalPayload(out, bodyRequest)
150+
151+
if err != nil {
152+
resp.Diagnostics.AddError("Unable to marshal payload", fmt.Sprintf("Unable to marshal payload: %s", err))
153+
return
154+
}
143155

144156
tflog.Info(ctx, fmt.Sprintf("Body Request: %s", out.String()))
145157

@@ -276,7 +288,12 @@ func (r *ModuleResource) Update(ctx context.Context, req resource.UpdateRequest,
276288
}
277289

278290
var out = new(bytes.Buffer)
279-
jsonapi.MarshalPayload(out, bodyRequest)
291+
err := jsonapi.MarshalPayload(out, bodyRequest)
292+
293+
if err != nil {
294+
resp.Diagnostics.AddError("Unable to marshal payload", fmt.Sprintf("Unable to marshal payload: %s", err))
295+
return
296+
}
280297

281298
moduleRequest, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/api/v1/organization/%s/module/%s", r.endpoint, state.OrganizationId.ValueString(), state.ID.ValueString()), strings.NewReader(out.String()))
282299
moduleRequest.Header.Add("Authorization", fmt.Sprintf("Bearer %s", r.token))

internal/provider/organization_data_source.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ func (d *OrganizationDataSource) Configure(ctx context.Context, req datasource.C
5252
return
5353
}
5454

55-
customTransport := http.DefaultTransport.(*http.Transport).Clone()
56-
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
57-
client := http.Client{Transport: customTransport}
58-
59-
d.client = &client
55+
if providerData.InsecureHttpClient {
56+
if custom, ok := http.DefaultTransport.(*http.Transport); ok {
57+
customTransport := custom.Clone()
58+
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
59+
d.client = &http.Client{Transport: customTransport}
60+
} else {
61+
d.client = &http.Client{}
62+
}
63+
} else {
64+
d.client = &http.Client{}
65+
}
6066
d.endpoint = providerData.Endpoint
6167
d.token = providerData.Token
6268

@@ -115,6 +121,11 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
115121

116122
orgs, err = jsonapi.UnmarshalManyPayload(strings.NewReader(string(body)), reflect.TypeOf(new(client.OrganizationEntity)))
117123

124+
if err != nil {
125+
resp.Diagnostics.AddError("Unable to unmarshal payload", fmt.Sprintf("Unable to marshal payload: %s", err))
126+
return
127+
}
128+
118129
for _, organization := range orgs {
119130
data, _ := organization.(*client.OrganizationEntity)
120131
state.ID = types.StringValue(data.ID)

internal/provider/provider.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@ type TerrakubeProvider struct {
2929

3030
// hashicupsProviderModel maps provider schema data to a Go type.
3131
type TerrakubeProviderModel struct {
32-
Endpoint types.String `tfsdk:"endpoint"`
33-
Token types.String `tfsdk:"token"`
34-
}
35-
36-
// ScaffoldingProviderModel describes the provider data model.
37-
type ScaffoldingProviderModel struct {
38-
Endpoint types.String `tfsdk:"endpoint"`
32+
Endpoint types.String `tfsdk:"endpoint"`
33+
Token types.String `tfsdk:"token"`
34+
InsecureHttpClient types.Bool `tfsdk:"insecure_http_client"`
3935
}
4036

4137
type TerrakubeConnectionData struct {
42-
Endpoint string
43-
Token string
38+
Endpoint string
39+
Token string
40+
InsecureHttpClient bool
4441
}
4542

4643
func New(version string) func() provider.Provider {
@@ -67,6 +64,10 @@ func (p *TerrakubeProvider) Schema(ctx context.Context, req provider.SchemaReque
6764
Required: true,
6865
Description: "Personal Access Token generated in Terrakube UI (https://docs.terrakube.io/user-guide/organizations/api-tokens)",
6966
},
67+
"insecure_http_client": schema.BoolAttribute{
68+
Optional: true,
69+
Description: "Disable https certificate validation",
70+
},
7071
},
7172
}
7273
}
@@ -108,6 +109,7 @@ func (p *TerrakubeProvider) Configure(ctx context.Context, req provider.Configur
108109

109110
endpoint := os.Getenv("TERRAKUBE_ENDPOINT")
110111
token := os.Getenv("TERRAKUBE_TOKEN")
112+
insecureHttpClient := false
111113

112114
if !config.Endpoint.IsNull() {
113115
endpoint = config.Endpoint.ValueString()
@@ -117,6 +119,10 @@ func (p *TerrakubeProvider) Configure(ctx context.Context, req provider.Configur
117119
token = config.Token.ValueString()
118120
}
119121

122+
if !config.InsecureHttpClient.IsNull() {
123+
insecureHttpClient = config.InsecureHttpClient.ValueBool()
124+
}
125+
120126
// If any of the expected configurations are missing, return
121127
// errors with provider-specific guidance.
122128

@@ -148,6 +154,7 @@ func (p *TerrakubeProvider) Configure(ctx context.Context, req provider.Configur
148154

149155
connection.Endpoint = endpoint
150156
connection.Token = token
157+
connection.InsecureHttpClient = insecureHttpClient
151158

152159
resp.DataSourceData = connection
153160
resp.ResourceData = connection

0 commit comments

Comments
 (0)