Skip to content

Commit

Permalink
Merge pull request #2 from artie-labs/dana/deployments-datasource
Browse files Browse the repository at this point in the history
Deployments datasource
  • Loading branch information
danafallon authored Jul 22, 2024
2 parents 50edda0 + 9a28717 commit deaa5f1
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 103 deletions.
28 changes: 28 additions & 0 deletions docs/data-sources/deployments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "artie_deployments Data Source - artie"
subcategory: ""
description: |-
Artie Deployments Data Source
---

# artie_deployments (Data Source)

Artie Deployments Data Source



<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `deployments` (Attributes List) (see [below for nested schema](#nestedatt--deployments))

<a id="nestedatt--deployments"></a>
### Nested Schema for `deployments`

Read-Only:

- `name` (String)
- `uuid` (String)
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ provider "artie" {

### Optional

- `endpoint` (String) Example provider attribute
- `api_key` (String) Artie API key
- `endpoint` (String) Artie API endpoint
18 changes: 18 additions & 0 deletions examples/deployments/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
artie = {
source = "artie.com/terraform/artie"
}
}
}

provider "artie" {
endpoint = "http://0.0.0.0:8000"
api_key = "api-key"
}

data "artie_deployments" "example" {}

output "deployments" {
value = data.artie_deployments.example.deployments
}
144 changes: 144 additions & 0 deletions internal/provider/deployments_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package provider

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

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

// NewDeploymentsDataSource is a helper function to simplify the provider implementation.
func NewDeploymentsDataSource() datasource.DataSource {
return &deploymentsDataSource{}
}

type deploymentsModel struct {
UUID string `tfsdk:"uuid"`
Name string `tfsdk:"name"`
}

type deploymentsDataSourceModel struct {
Deployments []deploymentsModel `tfsdk:"deployments"`
}

type deploymentsResponse struct {
Deployments []deploymentsModel `json:"items"`
}

// deploymentsDataSource is the data source implementation.
type deploymentsDataSource struct {
endpoint basetypes.StringValue
apiKey basetypes.StringValue
}

// Metadata returns the data source type name.
func (d *deploymentsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_deployments"
}

// Configure adds the provider configured client to the data source.
func (d *deploymentsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
if req.ProviderData == nil {
return
}

providerData, ok := req.ProviderData.(ArtieProviderModel)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.endpoint = providerData.Endpoint
d.apiKey = providerData.APIKey
}

func (d *deploymentsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Artie Deployments Data Source",
Attributes: map[string]schema.Attribute{
"deployments": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}

// Read refreshes the Terraform state with the latest data.
func (d *deploymentsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Info(ctx, fmt.Sprintf("Endpoint: %s", d.endpoint.ValueString()))
var state deploymentsDataSourceModel

apiReq, err := http.NewRequest("GET", fmt.Sprintf("%s/deployments", d.endpoint.ValueString()), nil)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Deployments", err.Error())
return
}

apiReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiKey.ValueString()))
apiResp, err := http.DefaultClient.Do(apiReq)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Deployments", err.Error())
return
}

if apiResp.StatusCode != http.StatusOK {
resp.Diagnostics.AddError("Unable to Read Deployments", fmt.Sprintf("Received status code %d", apiResp.StatusCode))
return
}

bodyBytes, err := io.ReadAll(apiResp.Body)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Deployments", err.Error())
return
}

var deploymentsResp deploymentsResponse
err = json.Unmarshal(bodyBytes, &deploymentsResp)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Deployments", err.Error())
return
}

for _, deployment := range deploymentsResp.Deployments {
deploymentState := deploymentsModel{
UUID: deployment.UUID,
Name: deployment.Name,
}
state.Deployments = append(state.Deployments, deploymentState)
}

// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
32 changes: 0 additions & 32 deletions internal/provider/example_data_source_test.go

This file was deleted.

56 changes: 0 additions & 56 deletions internal/provider/example_resource_test.go

This file was deleted.

15 changes: 9 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package provider

import (
"context"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/function"
Expand All @@ -30,6 +29,7 @@ type ArtieProvider struct {
// ArtieProviderModel describes the provider data model.
type ArtieProviderModel struct {
Endpoint types.String `tfsdk:"endpoint"`
APIKey types.String `tfsdk:"api_key"`
}

func (p *ArtieProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand All @@ -41,7 +41,11 @@ func (p *ArtieProvider) Schema(ctx context.Context, req provider.SchemaRequest,
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
MarkdownDescription: "Example provider attribute",
MarkdownDescription: "Artie API endpoint",
Optional: true,
},
"api_key": schema.StringAttribute{
MarkdownDescription: "Artie API key",
Optional: true,
},
},
Expand All @@ -60,10 +64,8 @@ func (p *ArtieProvider) Configure(ctx context.Context, req provider.ConfigureReq
// Configuration values are now available.
// if data.Endpoint.IsNull() { /* ... */ }

// Example client configuration for data sources and resources
client := http.DefaultClient
resp.DataSourceData = client
resp.ResourceData = client
resp.DataSourceData = data
resp.ResourceData = data
}

func (p *ArtieProvider) Resources(ctx context.Context) []func() resource.Resource {
Expand All @@ -75,6 +77,7 @@ func (p *ArtieProvider) Resources(ctx context.Context) []func() resource.Resourc
func (p *ArtieProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewExampleDataSource,
NewDeploymentsDataSource,
}
}

Expand Down
8 changes: 0 additions & 8 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)
Expand All @@ -17,9 +15,3 @@ import (
var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"artie": providerserver.NewProtocol6WithError(New("test")()),
}

func testAccPreCheck(t *testing.T) {
// You can add code here to run prior to any test case execution, for example assertions
// about the appropriate environment variables being set are common to see in a pre-check
// function.
}

0 comments on commit deaa5f1

Please sign in to comment.