|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 9 | +) |
| 10 | + |
| 11 | +var _ resource.ResourceWithConfigure = (*cloudProjectAlertingResource)(nil) |
| 12 | + |
| 13 | +func NewCloudProjectAlertingResource() resource.Resource { |
| 14 | + return &cloudProjectAlertingResource{} |
| 15 | +} |
| 16 | + |
| 17 | +type cloudProjectAlertingResource struct { |
| 18 | + config *Config |
| 19 | +} |
| 20 | + |
| 21 | +func (r *cloudProjectAlertingResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 22 | + resp.TypeName = req.ProviderTypeName + "_cloud_project_alerting" |
| 23 | +} |
| 24 | + |
| 25 | +func (d *cloudProjectAlertingResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 26 | + if req.ProviderData == nil { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + config, ok := req.ProviderData.(*Config) |
| 31 | + if !ok { |
| 32 | + resp.Diagnostics.AddError( |
| 33 | + "Unexpected Resource Configure Type", |
| 34 | + fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 35 | + ) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + d.config = config |
| 40 | +} |
| 41 | + |
| 42 | +func (d *cloudProjectAlertingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 43 | + resp.Schema = CloudProjectAlertingResourceSchema(ctx) |
| 44 | +} |
| 45 | + |
| 46 | +func (r *cloudProjectAlertingResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 47 | + var data, responseData CloudProjectAlertingModel |
| 48 | + |
| 49 | + // Read Terraform plan data into the model |
| 50 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 51 | + if resp.Diagnostics.HasError() { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting" |
| 56 | + if err := r.config.OVHClient.Post(endpoint, data.ToCreate(), &responseData); err != nil { |
| 57 | + resp.Diagnostics.AddError( |
| 58 | + fmt.Sprintf("Error calling Post %s", endpoint), |
| 59 | + err.Error(), |
| 60 | + ) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + responseData.MergeWith(&data) |
| 65 | + |
| 66 | + // Save data into Terraform state |
| 67 | + resp.Diagnostics.Append(resp.State.Set(ctx, &responseData)...) |
| 68 | +} |
| 69 | + |
| 70 | +func (r *cloudProjectAlertingResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 71 | + var data, responseData CloudProjectAlertingModel |
| 72 | + |
| 73 | + // Read Terraform prior state data into the model |
| 74 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 75 | + if resp.Diagnostics.HasError() { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString()) |
| 80 | + |
| 81 | + if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil { |
| 82 | + resp.Diagnostics.AddError( |
| 83 | + fmt.Sprintf("Error calling Get %s", endpoint), |
| 84 | + err.Error(), |
| 85 | + ) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + data.MergeWith(&responseData) |
| 90 | + |
| 91 | + // Save updated data into Terraform state |
| 92 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 93 | +} |
| 94 | + |
| 95 | +func (r *cloudProjectAlertingResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 96 | + var data, planData, responseData CloudProjectAlertingModel |
| 97 | + |
| 98 | + // Read Terraform plan data into the model |
| 99 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...) |
| 100 | + if resp.Diagnostics.HasError() { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + // Read Terraform prior state data into the model |
| 105 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 106 | + if resp.Diagnostics.HasError() { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + // Update resource |
| 111 | + endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString()) |
| 112 | + if err := r.config.OVHClient.Put(endpoint, planData.ToUpdate(), nil); err != nil { |
| 113 | + resp.Diagnostics.AddError( |
| 114 | + fmt.Sprintf("Error calling Put %s", endpoint), |
| 115 | + err.Error(), |
| 116 | + ) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + // Read updated resource |
| 121 | + endpoint = "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString()) |
| 122 | + if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil { |
| 123 | + resp.Diagnostics.AddError( |
| 124 | + fmt.Sprintf("Error calling Get %s", endpoint), |
| 125 | + err.Error(), |
| 126 | + ) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + responseData.MergeWith(&planData) |
| 131 | + |
| 132 | + // Save updated data into Terraform state |
| 133 | + resp.Diagnostics.Append(resp.State.Set(ctx, &responseData)...) |
| 134 | +} |
| 135 | + |
| 136 | +func (r *cloudProjectAlertingResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 137 | + var data CloudProjectAlertingModel |
| 138 | + |
| 139 | + // Read Terraform prior state data into the model |
| 140 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 141 | + |
| 142 | + if resp.Diagnostics.HasError() { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + // Delete API call logic |
| 147 | + endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString()) |
| 148 | + if err := r.config.OVHClient.Delete(endpoint, nil); err != nil { |
| 149 | + resp.Diagnostics.AddError( |
| 150 | + fmt.Sprintf("Error calling Delete %s", endpoint), |
| 151 | + err.Error(), |
| 152 | + ) |
| 153 | + } |
| 154 | +} |
0 commit comments