-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
…ema resource
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
cloudflare_api_shield_schema | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
page_title: "cloudflare_api_shield_schema Resource - Cloudflare" | ||
subcategory: "" | ||
description: |- | ||
Provides a resource to manage a schema in API Shield Schema Validation 2.0. | ||
--- | ||
|
||
# cloudflare_api_shield_schema (Resource) | ||
|
||
Provides a resource to manage a schema in API Shield Schema Validation 2.0. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "cloudflare_api_shield_schema" "petstore_schema" { | ||
zone_id = "0da42c8d2132a9ddaf714f9e7c920711" | ||
name = "myschema" | ||
kind = "openapi_v3" # optional | ||
validation_enabled = true # optional, default false | ||
source = file("./schemas/petstore.json") | ||
} | ||
``` | ||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) Name of the schema. **Modifying this attribute will force creation of a new resource.** | ||
- `source` (String) Schema file bytes. **Modifying this attribute will force creation of a new resource.** | ||
- `zone_id` (String) The zone identifier to target for the resource. **Modifying this attribute will force creation of a new resource.** | ||
|
||
### Optional | ||
|
||
- `kind` (String) Kind of schema. Defaults to `openapi_v3`. **Modifying this attribute will force creation of a new resource.** | ||
- `validation_enabled` (Boolean) Flag whether schema is enabled for validation. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
resource "cloudflare_api_shield_schema" "petstore_schema" { | ||
zone_id = "0da42c8d2132a9ddaf714f9e7c920711" | ||
name = "myschema" | ||
kind = "openapi_v3" # optional | ||
validation_enabled = true # optional, default false | ||
source = file("./schemas/petstore.json") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package sdkv2provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/MakeNowJust/heredoc/v2" | ||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func resourceCloudflareAPIShieldSchemas() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: resourceCloudflareAPIShieldSchemaSchema(), | ||
CreateContext: resourceCloudflareAPIShieldSchemaCreate, | ||
ReadContext: resourceCloudflareAPIShieldSchemaRead, | ||
DeleteContext: resourceCloudflareAPIShieldSchemaDelete, | ||
UpdateContext: resourceCloudflareAPIShieldSchemaUpdate, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: nil, | ||
}, | ||
Description: heredoc.Doc(` | ||
Provides a resource to manage a schema in API Shield Schema Validation 2.0. | ||
`), | ||
} | ||
} | ||
|
||
func resourceCloudflareAPIShieldSchemaCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
zoneID := d.Get(consts.ZoneIDSchemaKey).(string) | ||
|
||
sch, err := client.CreateAPIShieldSchema( | ||
ctx, | ||
cloudflare.ZoneIdentifier(zoneID), | ||
cloudflare.CreateAPIShieldSchemaParams{ | ||
Name: d.Get("name").(string), | ||
Kind: d.Get("kind").(string), | ||
Source: strings.NewReader(d.Get("source").(string)), | ||
ValidationEnabled: cloudflare.BoolPtr(d.Get("validation_enabled").(bool)), | ||
}, | ||
) | ||
|
||
if err != nil { | ||
return diag.FromErr(errors.Wrap(err, "failed to create cloudflare_api_shield_schema")) | ||
} | ||
|
||
// log warnings that occurred during creation | ||
for _, w := range sch.Events.Warnings { | ||
tflog.Warn(ctx, fmt.Sprintf("cloudflare_api_shield_schema: warning encountered when creating schema: %s", w)) | ||
} | ||
|
||
d.SetId(sch.Schema.ID) | ||
|
||
return resourceCloudflareAPIShieldSchemaRead(ctx, d, meta) | ||
} | ||
|
||
func resourceCloudflareAPIShieldSchemaRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
zoneID := d.Get(consts.ZoneIDSchemaKey).(string) | ||
|
||
sch, err := client.GetAPIShieldSchema( | ||
ctx, | ||
cloudflare.ZoneIdentifier(zoneID), | ||
cloudflare.GetAPIShieldSchemaParams{ | ||
SchemaID: d.Id(), | ||
}, | ||
) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("failed to fetch API Shield Schema: %w", err)) | ||
} | ||
|
||
if err := d.Set("name", sch.Name); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := d.Set("kind", sch.Kind); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := d.Set("source", sch.Source); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := d.Set("validation_enabled", sch.ValidationEnabled); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(sch.ID) | ||
return nil | ||
} | ||
|
||
func resourceCloudflareAPIShieldSchemaUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
zoneID := d.Get(consts.ZoneIDSchemaKey).(string) | ||
|
||
_, err := client.PatchAPIShieldSchema( | ||
Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / test
Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / tfproviderlint (ubuntu-latest)
Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / tfproviderlint (ubuntu-latest)
Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / tfproviderlint (ubuntu-latest)
|
||
ctx, | ||
cloudflare.ZoneIdentifier(zoneID), | ||
cloudflare.PatchAPIShieldSchemaParams{ | ||
Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / test
Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / tfproviderlint (ubuntu-latest)
Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / tfproviderlint (ubuntu-latest)
Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go GitHub Actions / tfproviderlint (ubuntu-latest)
|
||
SchemaID: d.Id(), | ||
ValidationEnabled: cloudflare.BoolPtr(d.Get("validation_enabled").(bool)), | ||
}, | ||
) | ||
|
||
if err != nil { | ||
return diag.FromErr(errors.Wrap(err, "failed to create API Shield Schema")) | ||
} | ||
|
||
return resourceCloudflareAPIShieldSchemaRead(ctx, d, meta) | ||
} | ||
|
||
func resourceCloudflareAPIShieldSchemaDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
zoneID := d.Get(consts.ZoneIDSchemaKey).(string) | ||
|
||
err := client.DeleteAPIShieldSchema( | ||
ctx, | ||
cloudflare.ZoneIdentifier(zoneID), | ||
cloudflare.DeleteAPIShieldSchemaParams{ | ||
SchemaID: d.Id(), | ||
}, | ||
) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("failed to fetch API Shield Schema: %w", err)) | ||
} | ||
|
||
return nil | ||
} |