|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package custom_pages |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/cloudflare/cloudflare-go/v4" |
| 12 | + "github.com/cloudflare/cloudflare-go/v4/custom_pages" |
| 13 | + "github.com/cloudflare/cloudflare-go/v4/option" |
| 14 | + "github.com/cloudflare/terraform-provider-cloudflare/internal/apijson" |
| 15 | + "github.com/cloudflare/terraform-provider-cloudflare/internal/logging" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 17 | +) |
| 18 | + |
| 19 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 20 | +var _ resource.ResourceWithConfigure = (*CustomPagesResource)(nil) |
| 21 | +var _ resource.ResourceWithModifyPlan = (*CustomPagesResource)(nil) |
| 22 | + |
| 23 | +func NewResource() resource.Resource { |
| 24 | + return &CustomPagesResource{} |
| 25 | +} |
| 26 | + |
| 27 | +// CustomPagesResource defines the resource implementation. |
| 28 | +type CustomPagesResource struct { |
| 29 | + client *cloudflare.Client |
| 30 | +} |
| 31 | + |
| 32 | +func (r *CustomPagesResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 33 | + resp.TypeName = req.ProviderTypeName + "_custom_pages" |
| 34 | +} |
| 35 | + |
| 36 | +func (r *CustomPagesResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 37 | + if req.ProviderData == nil { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + client, ok := req.ProviderData.(*cloudflare.Client) |
| 42 | + |
| 43 | + if !ok { |
| 44 | + resp.Diagnostics.AddError( |
| 45 | + "unexpected resource configure type", |
| 46 | + fmt.Sprintf("Expected *cloudflare.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 47 | + ) |
| 48 | + |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + r.client = client |
| 53 | +} |
| 54 | + |
| 55 | +func (r *CustomPagesResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 56 | + var data *CustomPagesModel |
| 57 | + |
| 58 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 59 | + |
| 60 | + if resp.Diagnostics.HasError() { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + dataBytes, err := data.MarshalJSON() |
| 65 | + if err != nil { |
| 66 | + resp.Diagnostics.AddError("failed to serialize http request", err.Error()) |
| 67 | + return |
| 68 | + } |
| 69 | + res := new(http.Response) |
| 70 | + env := CustomPagesResultEnvelope{*data} |
| 71 | + params := custom_pages.CustomPageUpdateParams{} |
| 72 | + |
| 73 | + if !data.AccountID.IsNull() { |
| 74 | + params.AccountID = cloudflare.F(data.AccountID.ValueString()) |
| 75 | + } else { |
| 76 | + params.ZoneID = cloudflare.F(data.ZoneID.ValueString()) |
| 77 | + } |
| 78 | + |
| 79 | + _, err = r.client.CustomPages.Update( |
| 80 | + ctx, |
| 81 | + data.Identifier.ValueString(), |
| 82 | + params, |
| 83 | + option.WithRequestBody("application/json", dataBytes), |
| 84 | + option.WithResponseBodyInto(&res), |
| 85 | + option.WithMiddleware(logging.Middleware(ctx)), |
| 86 | + ) |
| 87 | + if err != nil { |
| 88 | + resp.Diagnostics.AddError("failed to make http request", err.Error()) |
| 89 | + return |
| 90 | + } |
| 91 | + bytes, _ := io.ReadAll(res.Body) |
| 92 | + err = apijson.UnmarshalComputed(bytes, &env) |
| 93 | + if err != nil { |
| 94 | + resp.Diagnostics.AddError("failed to deserialize http request", err.Error()) |
| 95 | + return |
| 96 | + } |
| 97 | + data = &env.Result |
| 98 | + |
| 99 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 100 | +} |
| 101 | + |
| 102 | +func (r *CustomPagesResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 103 | + var data *CustomPagesModel |
| 104 | + |
| 105 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 106 | + |
| 107 | + if resp.Diagnostics.HasError() { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + var state *CustomPagesModel |
| 112 | + |
| 113 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 114 | + |
| 115 | + if resp.Diagnostics.HasError() { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + dataBytes, err := data.MarshalJSONForUpdate(*state) |
| 120 | + if err != nil { |
| 121 | + resp.Diagnostics.AddError("failed to serialize http request", err.Error()) |
| 122 | + return |
| 123 | + } |
| 124 | + res := new(http.Response) |
| 125 | + env := CustomPagesResultEnvelope{*data} |
| 126 | + params := custom_pages.CustomPageUpdateParams{} |
| 127 | + |
| 128 | + if !data.AccountID.IsNull() { |
| 129 | + params.AccountID = cloudflare.F(data.AccountID.ValueString()) |
| 130 | + } else { |
| 131 | + params.ZoneID = cloudflare.F(data.ZoneID.ValueString()) |
| 132 | + } |
| 133 | + |
| 134 | + _, err = r.client.CustomPages.Update( |
| 135 | + ctx, |
| 136 | + data.Identifier.ValueString(), |
| 137 | + params, |
| 138 | + option.WithRequestBody("application/json", dataBytes), |
| 139 | + option.WithResponseBodyInto(&res), |
| 140 | + option.WithMiddleware(logging.Middleware(ctx)), |
| 141 | + ) |
| 142 | + if err != nil { |
| 143 | + resp.Diagnostics.AddError("failed to make http request", err.Error()) |
| 144 | + return |
| 145 | + } |
| 146 | + bytes, _ := io.ReadAll(res.Body) |
| 147 | + err = apijson.UnmarshalComputed(bytes, &env) |
| 148 | + if err != nil { |
| 149 | + resp.Diagnostics.AddError("failed to deserialize http request", err.Error()) |
| 150 | + return |
| 151 | + } |
| 152 | + data = &env.Result |
| 153 | + |
| 154 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 155 | +} |
| 156 | + |
| 157 | +func (r *CustomPagesResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 158 | + var data *CustomPagesModel |
| 159 | + |
| 160 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 161 | + |
| 162 | + if resp.Diagnostics.HasError() { |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + res := new(http.Response) |
| 167 | + env := CustomPagesResultEnvelope{*data} |
| 168 | + params := custom_pages.CustomPageGetParams{} |
| 169 | + |
| 170 | + if !data.AccountID.IsNull() { |
| 171 | + params.AccountID = cloudflare.F(data.AccountID.ValueString()) |
| 172 | + } else { |
| 173 | + params.ZoneID = cloudflare.F(data.ZoneID.ValueString()) |
| 174 | + } |
| 175 | + |
| 176 | + _, err := r.client.CustomPages.Get( |
| 177 | + ctx, |
| 178 | + data.Identifier.ValueString(), |
| 179 | + params, |
| 180 | + option.WithResponseBodyInto(&res), |
| 181 | + option.WithMiddleware(logging.Middleware(ctx)), |
| 182 | + ) |
| 183 | + if res != nil && res.StatusCode == 404 { |
| 184 | + resp.Diagnostics.AddWarning("Resource not found", "The resource was not found on the server and will be removed from state.") |
| 185 | + resp.State.RemoveResource(ctx) |
| 186 | + return |
| 187 | + } |
| 188 | + if err != nil { |
| 189 | + resp.Diagnostics.AddError("failed to make http request", err.Error()) |
| 190 | + return |
| 191 | + } |
| 192 | + bytes, _ := io.ReadAll(res.Body) |
| 193 | + err = apijson.Unmarshal(bytes, &env) |
| 194 | + if err != nil { |
| 195 | + resp.Diagnostics.AddError("failed to deserialize http request", err.Error()) |
| 196 | + return |
| 197 | + } |
| 198 | + data = &env.Result |
| 199 | + |
| 200 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 201 | +} |
| 202 | + |
| 203 | +func (r *CustomPagesResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 204 | + |
| 205 | +} |
| 206 | + |
| 207 | +func (r *CustomPagesResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { |
| 208 | + if req.State.Raw.IsNull() { |
| 209 | + resp.Diagnostics.AddWarning( |
| 210 | + "Resource Destruction Considerations", |
| 211 | + "This resource cannot be destroyed from Terraform. If you create this resource, it will be "+ |
| 212 | + "present in the API until manually deleted.", |
| 213 | + ) |
| 214 | + } |
| 215 | + if req.Plan.Raw.IsNull() { |
| 216 | + resp.Diagnostics.AddWarning( |
| 217 | + "Resource Destruction Considerations", |
| 218 | + "Applying this resource destruction will remove the resource from the Terraform state "+ |
| 219 | + "but will not change it in the API. If you would like to destroy or reset this resource "+ |
| 220 | + "in the API, refer to the documentation for how to do it manually.", |
| 221 | + ) |
| 222 | + } |
| 223 | +} |
0 commit comments