Skip to content

Commit f0c4659

Browse files
committed
feature: source provider configuration from environment
1 parent 264a635 commit f0c4659

File tree

7 files changed

+66
-24
lines changed

7 files changed

+66
-24
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
## v0.7.1
2+
3+
FEATURES:
4+
- Source provider configuration from environment variables.
5+
6+
## v0.7.0
7+
8+
FEATURES:
9+
- Added `meshstack_building_block_v2` data source.
10+
- Added `meshstack_building_block_v2` resource.
11+
112
## v0.6.1
13+
214
REFACTOR:
315
- Validate success responses by checking for HTTP status codes in the 2xx range
416

client/client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func (c *MeshStackProviderClient) login() error {
8686

8787
res, err := c.httpClient.Do(req)
8888

89-
if err != nil || res.StatusCode != 200 {
89+
if err != nil {
90+
return err
91+
} else if res.StatusCode != 200 {
9092
return errors.New(ERROR_AUTHENTICATION_FAILURE)
9193
}
9294

@@ -174,8 +176,9 @@ func (c *MeshStackProviderClient) doAuthenticatedRequest(req *http.Request) (*ht
174176
log.Println(req)
175177

176178
// add authentication
177-
if c.ensureValidToken() != nil {
178-
return nil, errors.New(ERROR_AUTHENTICATION_FAILURE)
179+
err := c.ensureValidToken()
180+
if err != nil {
181+
return nil, err
179182
}
180183
req.Header.Set("Authorization", c.token)
181184

docs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: |-
77

88
# meshStack Provider
99

10-
The meshStack terraform provider is an open-source tool, licensed under the MPL-2.0, and is actively maintained by meshcloud GmbH. The provider leverages external APIs of meshStack to manage resources as code.
10+
The meshStack terraform provider is an open-source tool, licensed under the MPL-2.0, and is actively maintained by meshcloud GmbH. The provider exposes APIs of meshStack to manage resources as code.
1111

1212
## Example Usage
1313

@@ -21,6 +21,6 @@ provider "meshstack" {
2121

2222
### Required
2323

24-
- `apikey` (String) API Key to authenticate against the meshStack API
25-
- `apisecret` (String) API Secret to authenticate against the meshStack API
26-
- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io`
24+
- `apikey` (String) API Key to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_KEY`.
25+
- `apisecret` (String) API Secret to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_SECRET`.
26+
- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io`. Can be sourced from `MESHTACK_ENDPOINT`.

docs/resources/building_block_v2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Read-Only:
5252

5353
Required:
5454

55-
- `uuid` (String) UUID of the building block definition.
55+
- `uuid` (String) UUID of the building block definition version.
5656

5757

5858
<a id="nestedatt--spec--target_ref"></a>

internal/provider/building_block_v2_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (r *buildingBlockV2Resource) Schema(ctx context.Context, req resource.Schem
183183
Required: true,
184184
Attributes: map[string]schema.Attribute{
185185
"uuid": schema.StringAttribute{
186-
MarkdownDescription: "UUID of the building block definition.",
186+
MarkdownDescription: "UUID of the building block definition version.",
187187
Required: true,
188188
},
189189
},

internal/provider/provider.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"net/url"
6+
"os"
67

78
"github.com/meshcloud/terraform-provider-meshstack/client"
89

@@ -39,15 +40,15 @@ func (p *MeshStackProvider) Schema(ctx context.Context, req provider.SchemaReque
3940
Attributes: map[string]schema.Attribute{
4041
"endpoint": schema.StringAttribute{
4142
MarkdownDescription: "URl of meshStack API, e.g. `https://api.my.meshstack.io`",
42-
Required: true,
43+
Optional: true,
4344
},
4445
"apikey": schema.StringAttribute{
4546
MarkdownDescription: "API Key to authenticate against the meshStack API",
46-
Required: true,
47+
Optional: true,
4748
},
4849
"apisecret": schema.StringAttribute{
4950
MarkdownDescription: "API Secret to authenticate against the meshStack API",
50-
Required: true,
51+
Optional: true,
5152
},
5253
},
5354
}
@@ -57,21 +58,47 @@ func (p *MeshStackProvider) Configure(ctx context.Context, req provider.Configur
5758
var data MeshStackProviderModel
5859
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
5960

60-
url, err := url.Parse(data.Endpoint.ValueString())
61+
endpoint, ok := os.LookupEnv("MESHSTACK_ENDPOINT")
62+
if !ok {
63+
if data.Endpoint.IsNull() || data.Endpoint.IsUnknown() {
64+
resp.Diagnostics.AddError("Provider endpoint missing.", "Set provider.meshstack.endpoint or use MESHSTACK_ENDPOINT environment variable.")
65+
return
66+
}
67+
endpoint = data.Endpoint.ValueString()
68+
}
69+
70+
url, err := url.Parse(endpoint)
6171
if err != nil {
6272
resp.Diagnostics.AddError("Provider endpoint not valid.", "The value provided as the providers endpoint is not a valid URL.")
63-
} else {
64-
client, err := client.NewClient(url, data.ApiKey.ValueString(), data.ApiSecret.ValueString()) // TODO handle err
65-
if err != nil {
66-
resp.Diagnostics.AddError("Failed to create client.", err.Error())
73+
return
74+
}
75+
76+
apiKey, ok := os.LookupEnv("MESHSTACK_API_KEY")
77+
if !ok {
78+
if data.ApiKey.IsNull() || data.ApiKey.IsUnknown() {
79+
resp.Diagnostics.AddError("Provider API key missing.", "Set provider.meshstack.apikey or use MESHSTACK_API_KEY environment variable.")
80+
return
6781
}
68-
resp.DataSourceData = client
69-
resp.ResourceData = client
82+
apiKey = data.ApiKey.ValueString()
7083
}
7184

72-
if resp.Diagnostics.HasError() {
85+
apiSecret, ok := os.LookupEnv("MESHSTACK_API_SECRET")
86+
if !ok {
87+
if data.ApiSecret.IsNull() || data.ApiSecret.IsUnknown() {
88+
resp.Diagnostics.AddError("Provider API secret missing.", "Set provider.meshstack.apisecret or use MESHSTACK_API_SECRET environment variable.")
89+
return
90+
}
91+
apiSecret = data.ApiSecret.ValueString()
92+
}
93+
94+
client, err := client.NewClient(url, apiKey, apiSecret)
95+
if err != nil {
96+
resp.Diagnostics.AddError("Failed to create client.", err.Error())
7397
return
7498
}
99+
resp.DataSourceData = client
100+
resp.ResourceData = client
101+
75102
}
76103

77104
func (p *MeshStackProvider) Resources(ctx context.Context) []func() resource.Resource {

templates/index.md.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: |-
77

88
# meshStack Provider
99

10-
The meshStack terraform provider is an open-source tool, licensed under the MPL-2.0, and is actively maintained by meshcloud GmbH. The provider leverages external APIs of meshStack to manage resources as code.
10+
The meshStack terraform provider is an open-source tool, licensed under the MPL-2.0, and is actively maintained by meshcloud GmbH. The provider exposes APIs of meshStack to manage resources as code.
1111

1212
## Example Usage
1313

@@ -21,6 +21,6 @@ provider "meshstack" {
2121

2222
### Required
2323

24-
- `apikey` (String) API Key to authenticate against the meshStack API
25-
- `apisecret` (String) API Secret to authenticate against the meshStack API
26-
- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io`
24+
- `apikey` (String) API Key to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_KEY`.
25+
- `apisecret` (String) API Secret to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_SECRET`.
26+
- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io`. Can be sourced from `MESHTACK_ENDPOINT`.

0 commit comments

Comments
 (0)