Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 6 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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`.
2 changes: 1 addition & 1 deletion docs/resources/building_block_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Read-Only:

Required:

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


<a id="nestedatt--spec--target_ref"></a>
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/building_block_v2_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down
49 changes: 38 additions & 11 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"net/url"
"os"

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

Expand Down Expand Up @@ -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,
},
},
}
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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`.
Loading