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