|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/url" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" |
| 13 | + "github.com/ovh/terraform-provider-ovh/ovh/types" |
| 14 | +) |
| 15 | + |
| 16 | +var _ resource.ResourceWithConfigure = (*domainZoneImportResource)(nil) |
| 17 | + |
| 18 | +func NewDomainZoneImportResource() resource.Resource { |
| 19 | + return &domainZoneImportResource{} |
| 20 | +} |
| 21 | + |
| 22 | +type domainZoneImportResource struct { |
| 23 | + config *Config |
| 24 | +} |
| 25 | + |
| 26 | +func (r *domainZoneImportResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 27 | + resp.TypeName = req.ProviderTypeName + "_domain_zone_import" |
| 28 | +} |
| 29 | + |
| 30 | +func (d *domainZoneImportResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 31 | + if req.ProviderData == nil { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + config, ok := req.ProviderData.(*Config) |
| 36 | + if !ok { |
| 37 | + resp.Diagnostics.AddError( |
| 38 | + "Unexpected Resource Configure Type", |
| 39 | + fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 40 | + ) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + d.config = config |
| 45 | +} |
| 46 | + |
| 47 | +func (d *domainZoneImportResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 48 | + resp.Schema = DomainZoneImportResourceSchema(ctx) |
| 49 | +} |
| 50 | + |
| 51 | +func (r *domainZoneImportResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 52 | + var ( |
| 53 | + data DomainZoneImportModel |
| 54 | + task DomainZoneTask |
| 55 | + export string |
| 56 | + ) |
| 57 | + |
| 58 | + // Read Terraform plan data into the model |
| 59 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 60 | + if resp.Diagnostics.HasError() { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + // Import zone file |
| 65 | + endpoint := "/domain/zone/" + url.PathEscape(data.ZoneName.ValueString()) + "/import" |
| 66 | + if err := r.config.OVHClient.Post(endpoint, data.ToCreate(), &task); err != nil { |
| 67 | + resp.Diagnostics.AddError(fmt.Sprintf("Error calling Post %s", endpoint), err.Error()) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // Wait for import task completion |
| 72 | + if err := waitDNSTask(ctx, r.config, data.ZoneName.ValueString(), task.TaskID); err != nil { |
| 73 | + resp.Diagnostics.AddError("Error waiting for task completion", err.Error()) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // Export zone file |
| 78 | + endpoint = "/domain/zone/" + url.PathEscape(data.ZoneName.ValueString()) + "/export" |
| 79 | + if err := r.config.OVHClient.Get(endpoint, &export); err != nil { |
| 80 | + resp.Diagnostics.AddError(fmt.Sprintf("Error calling Get %s", endpoint), err.Error()) |
| 81 | + return |
| 82 | + } |
| 83 | + data.ExportedContent = types.NewTfStringValue(export) |
| 84 | + |
| 85 | + // Save data into Terraform state |
| 86 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 87 | +} |
| 88 | + |
| 89 | +func (r *domainZoneImportResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 90 | + var ( |
| 91 | + data DomainZoneImportModel |
| 92 | + responseData string |
| 93 | + ) |
| 94 | + |
| 95 | + // Read Terraform prior state data into the model |
| 96 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 97 | + if resp.Diagnostics.HasError() { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + endpoint := "/domain/zone/" + url.PathEscape(data.ZoneName.ValueString()) + "/export" |
| 102 | + if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil { |
| 103 | + resp.Diagnostics.AddError( |
| 104 | + fmt.Sprintf("Error calling Get %s", endpoint), |
| 105 | + err.Error(), |
| 106 | + ) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + // Force diff if records have been modified |
| 111 | + if data.ExportedContent.ValueString() != responseData { |
| 112 | + data.ZoneFile = types.NewTfStringValue(responseData) |
| 113 | + } |
| 114 | + |
| 115 | + // Save updated data into Terraform state |
| 116 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 117 | +} |
| 118 | + |
| 119 | +func (r *domainZoneImportResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 120 | + resp.Diagnostics.AddError("update should never happen", "this code should be unreachable") |
| 121 | +} |
| 122 | + |
| 123 | +func (r *domainZoneImportResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 124 | + var data DomainZoneImportModel |
| 125 | + |
| 126 | + // Read Terraform prior state data into the model |
| 127 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 128 | + |
| 129 | + if resp.Diagnostics.HasError() { |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + // Delete API call logic |
| 134 | + endpoint := "/domain/zone/" + url.PathEscape(data.ZoneName.ValueString()) + "/reset" |
| 135 | + if err := r.config.OVHClient.Post(endpoint, json.RawMessage(`{"minimized":false}`), nil); err != nil { |
| 136 | + resp.Diagnostics.AddError( |
| 137 | + fmt.Sprintf("Error calling Post %s", endpoint), |
| 138 | + err.Error(), |
| 139 | + ) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func waitDNSTask(ctx context.Context, config *Config, zoneName string, taskID int) error { |
| 144 | + endpoint := fmt.Sprintf("/domain/zone/%s/task/%d", url.PathEscape(zoneName), taskID) |
| 145 | + |
| 146 | + stateConf := &retry.StateChangeConf{ |
| 147 | + Pending: []string{"doing", "todo"}, |
| 148 | + Target: []string{"done"}, |
| 149 | + Refresh: func() (interface{}, string, error) { |
| 150 | + var task DomainZoneTask |
| 151 | + if err := config.OVHClient.Get(endpoint, &task); err != nil { |
| 152 | + log.Printf("[ERROR] couldn't fetch task %d: error: %v", taskID, err) |
| 153 | + return nil, "error", err |
| 154 | + } |
| 155 | + return task.Status, task.Status, nil |
| 156 | + }, |
| 157 | + Timeout: time.Hour, |
| 158 | + Delay: 10 * time.Second, |
| 159 | + MinTimeout: 3 * time.Second, |
| 160 | + } |
| 161 | + |
| 162 | + _, err := stateConf.WaitForStateContext(ctx) |
| 163 | + |
| 164 | + return err |
| 165 | +} |
0 commit comments