-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We definitely need to automate creating these as well.
- Loading branch information
1 parent
5e1c4ee
commit 145e9c5
Showing
4 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"terraform-provider-plural/internal/common" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
"terraform-provider-plural/internal/client" | ||
) | ||
|
||
var _ resource.Resource = &GlobalServiceResource{} | ||
var _ resource.ResourceWithImportState = &GlobalServiceResource{} | ||
|
||
func NewGlobalServiceResource() resource.Resource { | ||
return &GlobalServiceResource{} | ||
} | ||
|
||
// GlobalServiceResource defines the GlobalService resource implementation. | ||
type GlobalServiceResource struct { | ||
client *client.Client | ||
} | ||
|
||
func (r *GlobalServiceResource) Metadata( | ||
_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse, | ||
) { | ||
resp.TypeName = req.ProviderTypeName + "_global_service" | ||
} | ||
|
||
func (r *GlobalServiceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "GlobalService resource", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "Internal identifier of this GlobalService.", | ||
MarkdownDescription: "Internal identifier of this GlobalService.", | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.UseStateForUnknown(), | ||
}, | ||
}, | ||
"name": schema.StringAttribute{ | ||
Required: true, | ||
Description: "Name of this GlobalService.", | ||
MarkdownDescription: "Name of this GlobalService.", | ||
}, | ||
"distro": schema.StringAttribute{ | ||
Optional: true, | ||
Description: "Kubernetes distribution for this global servie, eg EKS, AKS, GKE, K3S.", | ||
}, | ||
"provider_id": schema.StringAttribute{ | ||
Optional: true, | ||
Description: "Id of a CAPI provider that this global service targets", | ||
MarkdownDescription: "Id of a CAPI provider that this global service targets.", | ||
}, | ||
"service_id": schema.StringAttribute{ | ||
Optional: true, | ||
Required: true, | ||
Description: "The id of the service that will be replicated by this global service.", | ||
MarkdownDescription: "The id of the service that will be replicated by this global service.", | ||
}, | ||
"tags": schema.MapAttribute{ | ||
Description: "Key-value tags used to target clusters for this global service.", | ||
MarkdownDescription: "Key-value tags used to target clusters for this global service.", | ||
Optional: true, | ||
ElementType: types.StringType, | ||
PlanModifiers: []planmodifier.Map{mapplanmodifier.RequiresReplace()}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *GlobalServiceResource) Configure( | ||
_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse, | ||
) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
data, ok := req.ProviderData.(*common.ProviderData) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Git Repository Resource Configure Type", | ||
fmt.Sprintf( | ||
"Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", | ||
req.ProviderData, | ||
), | ||
) | ||
|
||
return | ||
} | ||
|
||
r.client = data.Client | ||
} | ||
|
||
func (r *GlobalServiceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
data := new(globalService) | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
response, err := r.client.CreateGlobalService(ctx, data.Attributes(ctx, resp.Diagnostics)) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create GlobalService, got error: %s", err)) | ||
return | ||
} | ||
|
||
data.From(response.CreateGlobalService, resp.Diagnostics) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *GlobalServiceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
data := new(globalService) | ||
resp.Diagnostics.Append(req.State.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *GlobalServiceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
data := new(globalService) | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
_, err := r.client.UpdateGlobalService(ctx, data.Id.ValueString(), data.Attributes(ctx, resp.Diagnostics)) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update GlobalService, got error: %s", err)) | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *GlobalServiceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
data := new(globalService) | ||
resp.Diagnostics.Append(req.State.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
_, err := r.client.DeleteGlobalService(ctx, data.Id.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete GlobalService, got error: %s", err)) | ||
return | ||
} | ||
} | ||
|
||
func (r *GlobalServiceResource) ImportState( | ||
ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse, | ||
) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"terraform-provider-plural/internal/common" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
gqlclient "github.com/pluralsh/console-client-go" | ||
"github.com/samber/lo" | ||
) | ||
|
||
type globalService struct { | ||
Id types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
ServiceId types.String `tfsdk:"service_id"` | ||
Distro types.String `tfsdk:"distro"` | ||
ProviderId types.String `tfsdk:"provider_id"` | ||
Tags types.Map `tfsdk:"tags"` | ||
} | ||
|
||
func (g *globalService) From(response *gqlclient.GlobalServiceFragment, d diag.Diagnostics) { | ||
g.Id = types.StringValue(response.ID) | ||
g.Name = types.StringValue(response.Name) | ||
g.ServiceId = types.StringValue(response.Service.ID) | ||
if response.Distro != nil { | ||
g.Distro = types.StringValue(string(*response.Distro)) | ||
} | ||
if response.Provider != nil { | ||
g.ProviderId = types.StringValue(response.Provider.ID) | ||
} | ||
g.Tags = common.ClusterTagsFrom(response.Tags, d) | ||
} | ||
|
||
func (g *globalService) Attributes(ctx context.Context, d diag.Diagnostics) gqlclient.GlobalServiceAttributes { | ||
var distro *gqlclient.ClusterDistro | ||
if !g.Distro.IsNull() { | ||
distro = lo.ToPtr(gqlclient.ClusterDistro(strings.ToUpper(g.Distro.ValueString()))) | ||
} | ||
return gqlclient.GlobalServiceAttributes{ | ||
Name: g.Name.ValueString(), | ||
Distro: distro, | ||
ProviderID: g.ProviderId.ValueStringPointer(), | ||
Tags: g.TagsAttribute(ctx, d), | ||
} | ||
} | ||
|
||
func (g *globalService) TagsAttribute(ctx context.Context, d diag.Diagnostics) []*gqlclient.TagAttributes { | ||
result := make([]*gqlclient.TagAttributes, 0) | ||
elements := make(map[string]types.String, len(g.Tags.Elements())) | ||
d.Append(g.Tags.ElementsAs(ctx, &elements, false)...) | ||
|
||
for k, v := range elements { | ||
result = append(result, &gqlclient.TagAttributes{Name: k, Value: v.ValueString()}) | ||
} | ||
|
||
return result | ||
} |