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

TESTING: Flatten Interface Design #28

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions docs/resources/fastly_service_activation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "fastly_service_activation Resource - terraform-provider-fastly-framework"
subcategory: ""
description: |-
Testing
---

# fastly_service_activation (Resource)

Testing



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

### Required

- `id` (String) Alphanumeric string identifying the associated service resource
- `version` (Number) The associated service version to activate

### Optional

- `activate` (Boolean) Whether to activate the service (true) or to leave it inactive (false).


23 changes: 23 additions & 0 deletions docs/resources/fastly_thing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "fastly_thing Resource - terraform-provider-fastly-framework"
subcategory: ""
description: |-
Testing
---

# fastly_thing (Resource)

Testing



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

### Required

- `id` (String) Alphanumeric string identifying the associated service resource
- `version` (Number) The associated service version


15 changes: 15 additions & 0 deletions internal/provider/models/service_activation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models

import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

// ServiceActivation describes the resource data model.
type ServiceActivation struct {
// Activate controls whether the service should be activated.
Activate types.Bool `tfsdk:"activate"`
// ID is for the associated service resource.
ID types.String `tfsdk:"id"`
// Version is the service version to activate.
Version types.Int64 `tfsdk:"version"`
}
13 changes: 13 additions & 0 deletions internal/provider/models/thing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package models

import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Thing describes the resource data model.
type Thing struct {
// ID is for the associated service resource.
ID types.String `tfsdk:"id"`
// Version is the service version to activate.
Version types.Int64 `tfsdk:"version"`
}
5 changes: 5 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/integralist/terraform-provider-fastly-framework/internal/provider/resources/serviceactivation"
"github.com/integralist/terraform-provider-fastly-framework/internal/provider/resources/servicevcl"
"github.com/integralist/terraform-provider-fastly-framework/internal/provider/resources/thing"
)

// Ensure FastlyProvider satisfies various provider interfaces.
Expand Down Expand Up @@ -59,7 +62,9 @@ func (p *FastlyProvider) Configure(ctx context.Context, req provider.ConfigureRe

func (p *FastlyProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
serviceactivation.NewResource(),
servicevcl.NewResource(),
thing.NewResource(),
}
}

Expand Down
105 changes: 105 additions & 0 deletions internal/provider/resources/serviceactivation/activation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package serviceactivation

import (
"context"
_ "embed"
"fmt"

"github.com/fastly/fastly-go/fastly"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"

"github.com/integralist/terraform-provider-fastly-framework/internal/helpers"
)

//go:embed docs/service_activation.md
var resourceDescription string

// Ensure provider defined types fully satisfy framework interfaces
//
// https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/resource#Resource
// https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/resource#ResourceWithConfigure
var (
_ resource.Resource = &Resource{}
_ resource.ResourceWithConfigure = &Resource{}
)

// NewResource returns a new Terraform resource instance.
func NewResource() func() resource.Resource {
return func() resource.Resource {
return &Resource{}
}
}

// Resource defines the resource implementation.
type Resource struct {
// client is a preconfigured instance of the Fastly API client.
client *fastly.APIClient
// clientCtx contains the user's API token.
clientCtx context.Context
}

// Metadata should return the full name of the resource.
func (r *Resource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_service_activation"
}

// Schema should return the schema for this resource.
//
// NOTE: Some optional attributes are also 'computed' so we can set a default.
func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
attrs := map[string]schema.Attribute{
"activate": schema.BoolAttribute{
Computed: true,
MarkdownDescription: "Whether to activate the service (true) or to leave it inactive (false).",
Optional: true,
PlanModifiers: []planmodifier.Bool{
helpers.BoolDefaultModifier{Default: true},
},
},
"id": schema.StringAttribute{
Required: true,
MarkdownDescription: "Alphanumeric string identifying the associated service resource",
PlanModifiers: []planmodifier.String{
// UseStateForUnknown is useful for reducing (known after apply) plan
// outputs for computed attributes which are known to not change over time.
stringplanmodifier.UseStateForUnknown(),
},
},
"version": schema.Int64Attribute{
Required: true,
MarkdownDescription: "The associated service version to activate",
},
}

resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: resourceDescription,

// Attributes is the mapping of underlying attribute names to attribute definitions.
Attributes: attrs,
}
}

// Configure includes provider-level data or clients.
func (r *Resource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*fastly.APIClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *fastly.APIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.client = client
r.clientCtx = fastly.NewAPIKeyContextFromEnv("FASTLY_API_TOKEN")
}
46 changes: 46 additions & 0 deletions internal/provider/resources/serviceactivation/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package serviceactivation

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/integralist/terraform-provider-fastly-framework/internal/helpers"
"github.com/integralist/terraform-provider-fastly-framework/internal/provider/models"
)

// Create is called when the provider must create a new resource.
// Config and planned state values should be read from the CreateRequest.
// New state values set on the CreateResponse.
func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
api := helpers.API{
Client: r.client,
ClientCtx: r.clientCtx,
}
fmt.Printf("api client: %+v\n", api)

// TODO: Create the resource that will handle service activation.

// Store the planned changes so they can be saved into Terraform state.
var plan *models.ServiceActivation
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

if plan.Activate.ValueBool() {
fmt.Printf("plan.Activate.ValueBool(): %+v\n", plan.Activate.ValueBool())
// if err != nil {
// tflog.Trace(ctx, "Fastly VersionAPI.ActivateServiceVersion error", map[string]any{"http_resp": httpResp})
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to activate service version %d, got error: %s", 1, err))
// return
// }
}

// Save the planned changes into Terraform state.
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)

tflog.Trace(ctx, "ACTIVATION Create", map[string]any{"state": fmt.Sprintf("%+v", plan)})
}
32 changes: 32 additions & 0 deletions internal/provider/resources/serviceactivation/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package serviceactivation

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/integralist/terraform-provider-fastly-framework/internal/provider/models"
)

// Delete is called when the provider must delete the resource.
// Config values may be read from the DeleteRequest.
//
// If execution completes without error, the framework will automatically call
// DeleteResponse.State.RemoveResource().
func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state *models.ServiceActivation

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

if state.Activate.ValueBool() {
fmt.Printf("state.Activate.ValueBool(): %+v\n", state.Activate.ValueBool())
}

tflog.Trace(ctx, "ACTIVATION Delete", map[string]any{"state": fmt.Sprintf("%+v", state)})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing
28 changes: 28 additions & 0 deletions internal/provider/resources/serviceactivation/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package serviceactivation

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/integralist/terraform-provider-fastly-framework/internal/provider/models"
)

// Read is called when the provider must read resource values in order to update state.
// Planned state values should be read from the ReadRequest.
// New state values set on the ReadResponse.
func (r *Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Store the prior state (if any) so it can later be mutated and saved back into state.
var state *models.ServiceActivation
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

// Save the updated state data back into Terraform state.
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)

tflog.Trace(ctx, "ACTIVATION Read", map[string]any{"state": fmt.Sprintf("%+v", state)})
}
35 changes: 35 additions & 0 deletions internal/provider/resources/serviceactivation/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package serviceactivation

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/integralist/terraform-provider-fastly-framework/internal/provider/models"
)

// Update is called to update the state of the resource.
// Config, planned state, and prior state values should be read from the UpdateRequest.
// New state values set on the UpdateResponse.
func (r *Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan *models.ServiceActivation
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

var state *models.ServiceActivation
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

// TODO: identify changes and action them

// Save the planned changes into Terraform state.
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)

tflog.Trace(ctx, "ACTIVATION Update", map[string]any{"state": fmt.Sprintf("%+v", plan)})
}
2 changes: 2 additions & 0 deletions internal/provider/resources/servicevcl/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/integralist/terraform-provider-fastly-framework/internal/helpers"
"github.com/integralist/terraform-provider-fastly-framework/internal/provider/interfaces"
"github.com/integralist/terraform-provider-fastly-framework/internal/provider/resources/domain"
Expand Down Expand Up @@ -70,6 +71,7 @@ func (r *Resource) Metadata(_ context.Context, req resource.MetadataRequest, res
//
// NOTE: Some optional attributes are also 'computed' so we can set a default.
func (r *Resource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
// Return the common attributes across both VCL and Compute services.
attrs := schemas.Service()

attrs["default_ttl"] = schema.Int64Attribute{
Expand Down
Loading