|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + unleash "github.com/Unleash/unleash-server-api-go/client" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + _ resource.Resource = &environmentResource{} |
| 18 | + _ resource.ResourceWithConfigure = &environmentResource{} |
| 19 | + _ resource.ResourceWithImportState = &environmentResource{} |
| 20 | +) |
| 21 | + |
| 22 | +func NewEnvironmentResource() resource.Resource { |
| 23 | + return &environmentResource{} |
| 24 | +} |
| 25 | + |
| 26 | +type environmentResource struct { |
| 27 | + client *unleash.APIClient |
| 28 | +} |
| 29 | + |
| 30 | +type environmentResourceModel struct { |
| 31 | + Name types.String `tfsdk:"name"` |
| 32 | + Type types.String `tfsdk:"type"` |
| 33 | +} |
| 34 | + |
| 35 | +func (r *environmentResource) Configure(ctx context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { |
| 36 | + if req.ProviderData == nil { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + client, ok := req.ProviderData.(*unleash.APIClient) |
| 41 | + if !ok { |
| 42 | + return |
| 43 | + } |
| 44 | + r.client = client |
| 45 | +} |
| 46 | + |
| 47 | +func (r *environmentResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 48 | + resp.TypeName = req.ProviderTypeName + "_environment" |
| 49 | +} |
| 50 | + |
| 51 | +func (r *environmentResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 52 | + resp.Schema = schema.Schema{ |
| 53 | + Description: "Fetch a context field.", |
| 54 | + Attributes: map[string]schema.Attribute{ |
| 55 | + "name": schema.StringAttribute{ |
| 56 | + Description: "The name of the environment. Must be a URL-friendly string according to RFC 3968. " + |
| 57 | + "Changing this property will require the resource to be replaced, it's generally safer to remove this resource and create a new one.", |
| 58 | + Required: true, |
| 59 | + PlanModifiers: []planmodifier.String{ |
| 60 | + stringplanmodifier.RequiresReplace(), |
| 61 | + }, |
| 62 | + }, |
| 63 | + "type": schema.StringAttribute{ |
| 64 | + Description: "The type of the environment. Unleash recognizes 'development', 'test', 'preproduction' and 'production'. " + |
| 65 | + "You can pass other values and Unleash will accept them but they will carry no special semantics.", |
| 66 | + Required: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func (r *environmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 73 | + tflog.Debug(ctx, "Preparing to import environment resource") |
| 74 | + |
| 75 | + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) |
| 76 | + |
| 77 | + tflog.Debug(ctx, "Finished importing environment data source", map[string]interface{}{"success": true}) |
| 78 | +} |
| 79 | + |
| 80 | +func (r *environmentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 81 | + tflog.Debug(ctx, "Preparing to create environment resource") |
| 82 | + var plan environmentResourceModel |
| 83 | + |
| 84 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) |
| 85 | + if resp.Diagnostics.HasError() { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + createEnvironmentRequest := *unleash.NewCreateEnvironmentSchemaWithDefaults() |
| 90 | + createEnvironmentRequest.Name = plan.Name.ValueString() |
| 91 | + createEnvironmentRequest.Type = plan.Type.ValueString() |
| 92 | + |
| 93 | + environment, apiResponse, err := r.client.EnvironmentsAPI.CreateEnvironment(ctx).CreateEnvironmentSchema(createEnvironmentRequest).Execute() |
| 94 | + |
| 95 | + if !ValidateApiResponse(apiResponse, 201, &resp.Diagnostics, err) { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + plan = environmentResourceModel{ |
| 100 | + Name: types.StringValue(environment.Name), |
| 101 | + Type: types.StringValue(environment.Type), |
| 102 | + } |
| 103 | + |
| 104 | + resp.State.Set(ctx, &plan) |
| 105 | + tflog.Debug(ctx, "Finished creating environment resource", map[string]interface{}{"success": true}) |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +func (r *environmentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 110 | + tflog.Debug(ctx, "Preparing to read environment resource") |
| 111 | + var state environmentResourceModel |
| 112 | + |
| 113 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 114 | + if resp.Diagnostics.HasError() { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + environment, apiResponse, err := r.client.EnvironmentsAPI.GetEnvironment(ctx, state.Name.ValueString()).Execute() |
| 119 | + |
| 120 | + if !ValidateApiResponse(apiResponse, 200, &resp.Diagnostics, err) { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + state = environmentResourceModel{ |
| 125 | + Name: types.StringValue(environment.Name), |
| 126 | + Type: types.StringValue(environment.Type), |
| 127 | + } |
| 128 | + |
| 129 | + resp.State.Set(ctx, &state) |
| 130 | + |
| 131 | + tflog.Debug(ctx, "Finished reading environment resource", map[string]interface{}{"success": true}) |
| 132 | +} |
| 133 | + |
| 134 | +func (r *environmentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 135 | + tflog.Debug(ctx, "Preparing to update environment resource") |
| 136 | + var plan environmentResourceModel |
| 137 | + |
| 138 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) |
| 139 | + if resp.Diagnostics.HasError() { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + updateEnvironmentRequest := *unleash.NewUpdateEnvironmentSchemaWithDefaults() |
| 144 | + updateEnvironmentRequest.SetType(plan.Type.ValueString()) |
| 145 | + |
| 146 | + environment, apiResponse, err := r.client.EnvironmentsAPI.UpdateEnvironment(ctx, plan.Name.ValueString()).UpdateEnvironmentSchema(updateEnvironmentRequest).Execute() |
| 147 | + |
| 148 | + if !ValidateApiResponse(apiResponse, 200, &resp.Diagnostics, err) { |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + plan = environmentResourceModel{ |
| 153 | + Name: types.StringValue(environment.Name), |
| 154 | + Type: types.StringValue(environment.Type), |
| 155 | + } |
| 156 | + |
| 157 | + resp.State.Set(ctx, &plan) |
| 158 | + tflog.Debug(ctx, "Finished updating environment resource", map[string]interface{}{"success": true}) |
| 159 | +} |
| 160 | + |
| 161 | +func (r *environmentResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 162 | + tflog.Debug(ctx, "Preparing to delete environment resource") |
| 163 | + var state environmentResourceModel |
| 164 | + |
| 165 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 166 | + if resp.Diagnostics.HasError() { |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + apiResponse, err := r.client.EnvironmentsAPI.RemoveEnvironment(ctx, state.Name.ValueString()).Execute() |
| 171 | + |
| 172 | + if !ValidateApiResponse(apiResponse, 200, &resp.Diagnostics, err) { |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + resp.State.RemoveResource(ctx) |
| 177 | + tflog.Debug(ctx, "Finished deleting environment resource", map[string]interface{}{"success": true}) |
| 178 | +} |
0 commit comments