Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create base provider files #2

Merged
merged 4 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added environment data source
  • Loading branch information
freimer committed Oct 12, 2022
commit c764627a0ccf54dc3f255933006819845bd4bcc1
149 changes: 149 additions & 0 deletions airplanedev/environment_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package airplanedev

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/airplanedev/cli/pkg/api"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &environmentDataSource{}
_ datasource.DataSourceWithConfigure = &environmentDataSource{}
)

type environmentDataSource struct {
client *api.Client
}

// environmentDataSourceModel maps the data source schema data.
type environmentDataSourceModel struct {
ID types.String `tfsdk:"id"`
Slug types.String `tfsdk:"slug"`
Name types.String `tfsdk:"name"`
TeamID types.String `tfsdk:"team_id"`
Default types.Bool `tfsdk:"default"`
CreatedAt types.String `tfsdk:"created_at"`
CreatedBy types.String `tfsdk:"created_by"`
UpdatedAt types.String `tfsdk:"updated_at"`
UpdatedBy types.String `tfsdk:"updated_by"`
IsArchived types.Bool `tfsdk:"is_archived"`
ArchivedAt types.String `tfsdk:"archived_at"`
}

func NewEnvironmentDataSource() datasource.DataSource {
return &environmentDataSource{}
}

// Configure adds the provider configured client to the data source.
func (d *environmentDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

d.client = req.ProviderData.(*api.Client)
}

func (d *environmentDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_environment"
}

// GetSchema defines the schema for the data source.
func (d *environmentDataSource) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"id": {
Type: types.StringType,
Computed: true,
},
"slug": {
Type: types.StringType,
Required: true,
},
"name": {
Type: types.StringType,
Computed: true,
},
"team_id": {
Type: types.StringType,
Computed: true,
},
"default": {
Type: types.BoolType,
Computed: true,
},
"created_at": {
Type: types.StringType,
Computed: true,
},
"created_by": {
Type: types.StringType,
Computed: true,
},
"updated_at": {
Type: types.StringType,
Computed: true,
},
"updated_by": {
Type: types.StringType,
Computed: true,
},
"is_archived": {
Type: types.BoolType,
Computed: true,
},
"archived_at": {
Type: types.StringType,
Computed: true,
},
},
}, nil
}

// Read refreshes the Terraform state with the latest data.
func (d *environmentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state environmentDataSourceModel

// Retrieve values from config
var slug string
diags := req.Config.GetAttribute(ctx, path.Root("slug"), &slug)
env, err := d.client.GetEnv(ctx, slug)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Airplane.dev Environment",
err.Error(),
)
return
}

// Map response body to model
state = environmentDataSourceModel{
ID: types.String{Value: env.ID},
Slug: types.String{Value: env.Slug},
Name: types.String{Value: env.Name},
TeamID: types.String{Value: env.TeamID},
Default: types.Bool{Value: env.Default},
CreatedAt: types.String{Value: env.CreatedAt.String()},
CreatedBy: types.String{Value: env.CreatedBy},
UpdatedAt: types.String{Value: env.UpdatedAt.String()},
UpdatedBy: types.String{Value: env.UpdatedBy},
IsArchived: types.Bool{Value: env.IsArchived},
ArchivedAt: types.String{Null: true},
}
if env.IsArchived {
state.ArchivedAt = types.String{Null: false, Value: env.ArchivedAt.String()}
}

// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
101 changes: 45 additions & 56 deletions airplanedev/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/airplanedev/cli/pkg/api"
)

// Ensure the implementation satisfies the expected interfaces
Expand All @@ -28,9 +30,9 @@ type airplanedevProvider struct{}

// airplanedevProviderModel maps provider schema data to a Go type.
type airplanedevProviderModel struct {
Host types.String `tfsdk:"host"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
Host types.String `tfsdk:"host"`
APIKey types.String `tfsdk:"api_key"`
TeamID types.String `tfsdk:"team_id"`
}

// Metadata returns the provider type name.
Expand All @@ -46,11 +48,12 @@ func (p *airplanedevProvider) GetSchema(_ context.Context) (tfsdk.Schema, diag.D
Type: types.StringType,
Optional: true,
},
"username": {
Type: types.StringType,
Optional: true,
"api_key": {
Type: types.StringType,
Optional: true,
Sensitive: true,
},
"password": {
"team_id": {
Type: types.StringType,
Optional: true,
Sensitive: true,
Expand All @@ -74,27 +77,27 @@ func (p *airplanedevProvider) Configure(ctx context.Context, req provider.Config
if config.Host.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("host"),
"Unknown Airplanedev API Host",
"The provider cannot create the Airplanedev API client as there is an unknown configuration value for the Airplanedev API host. "+
"Unknown Airplanedev API Host (host)",
"The provider cannot create the Airplanedev API client as there is an unknown configuration value for the Airplanedev API Host. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the AIRPLANEDEV_HOST environment variable.",
)
}

if config.Username.IsUnknown() {
if config.APIKey.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("username"),
"Unknown Airplanedev API Username",
"The provider cannot create the Airplanedev API client as there is an unknown configuration value for the Airplanedev API username. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the AIRPLANEDEV_USERNAME environment variable.",
path.Root("api_key"),
"Unknown Airplanedev APIKey (api_key)",
"The provider cannot create the Airplanedev API client as there is an unknown configuration value for the Airplanedev APIKey. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the AIRPLANEDEV_APIKEY environment variable.",
)
}

if config.Password.IsUnknown() {
if config.TeamID.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("password"),
"Unknown Airplanedev API Password",
"The provider cannot create the Airplanedev API client as there is an unknown configuration value for the Airplanedev API password. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the AIRPLANEDEV_PASSWORD environment variable.",
path.Root("team_id"),
"Unknown Airplanedev TeamID (team_id)",
"The provider cannot create the Airplanedev API client as there is an unknown configuration value for the Airplanedev TeamID. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the AIRPLANEDEV_TEAMID environment variable.",
)
}

Expand All @@ -106,50 +109,40 @@ func (p *airplanedevProvider) Configure(ctx context.Context, req provider.Config
// with Terraform configuration value if set.

host := os.Getenv("AIRPLANEDEV_HOST")
username := os.Getenv("AIRPLANEDEV_USERNAME")
password := os.Getenv("AIRPLANEDEV_PASSWORD")
apiKey := os.Getenv("AIRPLANEDEV_APIKEY")
teamID := os.Getenv("AIRPLANEDEV_TEAMID")

if !config.Host.IsNull() {
host = config.Host.Value
}

if !config.Username.IsNull() {
username = config.Username.Value
if !config.APIKey.IsNull() {
apiKey = config.APIKey.Value
}

if !config.Password.IsNull() {
password = config.Password.Value
if !config.TeamID.IsNull() {
teamID = config.TeamID.Value
}

// If any of the expected configurations are missing, return
// errors with provider-specific guidance.

if host == "" {
if apiKey == "" {
resp.Diagnostics.AddAttributeError(
path.Root("host"),
"Missing Airplanedev API Host",
"The provider cannot create the Airplanedev API client as there is a missing or empty value for the Airplanedev API host. "+
"Set the host value in the configuration or use the AIRPLANEDEV_HOST environment variable. "+
path.Root("api_key"),
"Missing Airplanedev APIKey (api_key)",
"The provider cannot create the Airplanedev API client as there is a missing or empty value for the Airplanedev APIKey. "+
"Set the api_key value in the configuration or use the AIRPLANEDEV_APIKEY environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}

if username == "" {
if teamID == "" {
resp.Diagnostics.AddAttributeError(
path.Root("username"),
"Missing Airplanedev API Username",
"The provider cannot create the Airplanedev API client as there is a missing or empty value for the Airplanedev API username. "+
"Set the username value in the configuration or use the AIRPLANEDEV_USERNAME environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}

if password == "" {
resp.Diagnostics.AddAttributeError(
path.Root("password"),
"Missing Airplanedev API Password",
"The provider cannot create the Airplanedev API client as there is a missing or empty value for the Airplanedev API password. "+
"Set the password value in the configuration or use the AIRPLANEDEV_PASSWORD environment variable. "+
path.Root("team_id"),
"Missing Airplanedev TeamID (team_id)",
"The provider cannot create the Airplanedev API client as there is a missing or empty value for the Airplanedev TeamID. "+
"Set the team_id value in the configuration or use the AIRPLANEDEV_TEAMID environment variable. "+
"If either is already set, ensure the value is not empty.",
)
}
Expand All @@ -159,16 +152,10 @@ func (p *airplanedevProvider) Configure(ctx context.Context, req provider.Config
}

// Create a new Airplanedev client using the configuration values
client, err := airplanedev.NewClient(&host, &username, &password)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Airplanedev API Client",
"An unexpected error occurred when creating the Airplanedev API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"Airplanedev Client Error: "+err.Error(),
)
return
}
client := &api.Client{
Host: host,
APIKey: apiKey,
TeamID: teamID}

// Make the Airplanedev client available during DataSource and Resource
// type Configure methods.
Expand All @@ -178,7 +165,9 @@ func (p *airplanedevProvider) Configure(ctx context.Context, req provider.Config

// DataSources defines the data sources implemented in the provider.
func (p *airplanedevProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return nil
return []func() datasource.DataSource{
NewEnvironmentDataSource,
}
}

// Resources defines the resources implemented in the provider.
Expand Down
8 changes: 7 additions & 1 deletion examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ terraform {

provider "airplanedev" {}

data "airplanedev_environment" "dev" {}
data "airplanedev_environment" "dev" {
slug = "dev"
}

output "dev_environment" {
value = data.airplanedev_environment.dev
}
Loading