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