diff --git a/CHANGELOG.md b/CHANGELOG.md index 3eac3cb..a736c58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,16 @@ +## v0.7.1 + +FEATURES: +- Source provider configuration from environment variables. + +## v0.7.0 + +FEATURES: +- Added `meshstack_building_block_v2` data source. +- Added `meshstack_building_block_v2` resource. + ## v0.6.1 + REFACTOR: - Validate success responses by checking for HTTP status codes in the 2xx range diff --git a/client/client.go b/client/client.go index bf0ee23..c6c629e 100644 --- a/client/client.go +++ b/client/client.go @@ -86,7 +86,9 @@ func (c *MeshStackProviderClient) login() error { res, err := c.httpClient.Do(req) - if err != nil || res.StatusCode != 200 { + if err != nil { + return err + } else if res.StatusCode != 200 { return errors.New(ERROR_AUTHENTICATION_FAILURE) } @@ -174,8 +176,9 @@ func (c *MeshStackProviderClient) doAuthenticatedRequest(req *http.Request) (*ht log.Println(req) // add authentication - if c.ensureValidToken() != nil { - return nil, errors.New(ERROR_AUTHENTICATION_FAILURE) + err := c.ensureValidToken() + if err != nil { + return nil, err } req.Header.Set("Authorization", c.token) diff --git a/docs/index.md b/docs/index.md index 660ed70..7c3aea1 100644 --- a/docs/index.md +++ b/docs/index.md @@ -7,7 +7,7 @@ description: |- # meshStack Provider -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. +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. ## Example Usage @@ -21,6 +21,6 @@ provider "meshstack" { ### Required -- `apikey` (String) API Key to authenticate against the meshStack API -- `apisecret` (String) API Secret to authenticate against the meshStack API -- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io` +- `apikey` (String) API Key to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_KEY`. +- `apisecret` (String) API Secret to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_SECRET`. +- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io`. Can be sourced from `MESHTACK_ENDPOINT`. diff --git a/docs/resources/building_block_v2.md b/docs/resources/building_block_v2.md index 63b1a18..6ababc9 100644 --- a/docs/resources/building_block_v2.md +++ b/docs/resources/building_block_v2.md @@ -52,7 +52,7 @@ Read-Only: Required: -- `uuid` (String) UUID of the building block definition. +- `uuid` (String) UUID of the building block definition version. diff --git a/internal/provider/building_block_v2_resource.go b/internal/provider/building_block_v2_resource.go index d61e517..ba916a2 100644 --- a/internal/provider/building_block_v2_resource.go +++ b/internal/provider/building_block_v2_resource.go @@ -183,7 +183,7 @@ func (r *buildingBlockV2Resource) Schema(ctx context.Context, req resource.Schem Required: true, Attributes: map[string]schema.Attribute{ "uuid": schema.StringAttribute{ - MarkdownDescription: "UUID of the building block definition.", + MarkdownDescription: "UUID of the building block definition version.", Required: true, }, }, diff --git a/internal/provider/provider.go b/internal/provider/provider.go index abe7c7f..94fe699 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -3,6 +3,7 @@ package provider import ( "context" "net/url" + "os" "github.com/meshcloud/terraform-provider-meshstack/client" @@ -39,15 +40,15 @@ func (p *MeshStackProvider) Schema(ctx context.Context, req provider.SchemaReque Attributes: map[string]schema.Attribute{ "endpoint": schema.StringAttribute{ MarkdownDescription: "URl of meshStack API, e.g. `https://api.my.meshstack.io`", - Required: true, + Optional: true, }, "apikey": schema.StringAttribute{ MarkdownDescription: "API Key to authenticate against the meshStack API", - Required: true, + Optional: true, }, "apisecret": schema.StringAttribute{ MarkdownDescription: "API Secret to authenticate against the meshStack API", - Required: true, + Optional: true, }, }, } @@ -57,21 +58,47 @@ func (p *MeshStackProvider) Configure(ctx context.Context, req provider.Configur var data MeshStackProviderModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - url, err := url.Parse(data.Endpoint.ValueString()) + endpoint, ok := os.LookupEnv("MESHSTACK_ENDPOINT") + if !ok { + if data.Endpoint.IsNull() || data.Endpoint.IsUnknown() { + resp.Diagnostics.AddError("Provider endpoint missing.", "Set provider.meshstack.endpoint or use MESHSTACK_ENDPOINT environment variable.") + return + } + endpoint = data.Endpoint.ValueString() + } + + url, err := url.Parse(endpoint) if err != nil { resp.Diagnostics.AddError("Provider endpoint not valid.", "The value provided as the providers endpoint is not a valid URL.") - } else { - client, err := client.NewClient(url, data.ApiKey.ValueString(), data.ApiSecret.ValueString()) // TODO handle err - if err != nil { - resp.Diagnostics.AddError("Failed to create client.", err.Error()) + return + } + + apiKey, ok := os.LookupEnv("MESHSTACK_API_KEY") + if !ok { + if data.ApiKey.IsNull() || data.ApiKey.IsUnknown() { + resp.Diagnostics.AddError("Provider API key missing.", "Set provider.meshstack.apikey or use MESHSTACK_API_KEY environment variable.") + return } - resp.DataSourceData = client - resp.ResourceData = client + apiKey = data.ApiKey.ValueString() } - if resp.Diagnostics.HasError() { + apiSecret, ok := os.LookupEnv("MESHSTACK_API_SECRET") + if !ok { + if data.ApiSecret.IsNull() || data.ApiSecret.IsUnknown() { + resp.Diagnostics.AddError("Provider API secret missing.", "Set provider.meshstack.apisecret or use MESHSTACK_API_SECRET environment variable.") + return + } + apiSecret = data.ApiSecret.ValueString() + } + + client, err := client.NewClient(url, apiKey, apiSecret) + if err != nil { + resp.Diagnostics.AddError("Failed to create client.", err.Error()) return } + resp.DataSourceData = client + resp.ResourceData = client + } func (p *MeshStackProvider) Resources(ctx context.Context) []func() resource.Resource { diff --git a/templates/index.md.tmpl b/templates/index.md.tmpl index 660ed70..7c3aea1 100644 --- a/templates/index.md.tmpl +++ b/templates/index.md.tmpl @@ -7,7 +7,7 @@ description: |- # meshStack Provider -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. +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. ## Example Usage @@ -21,6 +21,6 @@ provider "meshstack" { ### Required -- `apikey` (String) API Key to authenticate against the meshStack API -- `apisecret` (String) API Secret to authenticate against the meshStack API -- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io` +- `apikey` (String) API Key to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_KEY`. +- `apisecret` (String) API Secret to authenticate against the meshStack API. Can be sourced from `MESHSTACK_API_SECRET`. +- `endpoint` (String) URl of meshStack API, e.g. `https://api.my.meshstack.io`. Can be sourced from `MESHTACK_ENDPOINT`.