Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/cloudflare_api_shield_schema: add API Shield Schema resource #2784

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2784.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
cloudflare_api_shield_schema
```
41 changes: 41 additions & 0 deletions docs/resources/api_shield_schema.md
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.


7 changes: 7 additions & 0 deletions examples/resources/cloudflare_api_shield_schema/resource.tf
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")
}
1 change: 1 addition & 0 deletions internal/sdkv2provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func New(version string) func() *schema.Provider {
"cloudflare_address_map": resourceCloudflareAddressMap(),
"cloudflare_api_shield": resourceCloudflareAPIShield(),
"cloudflare_api_shield_operation": resourceCloudflareAPIShieldOperation(),
"cloudflare_api_shield_schema": resourceCloudflareAPIShieldSchemas(),
"cloudflare_api_token": resourceCloudflareApiToken(),
"cloudflare_argo": resourceCloudflareArgo(),
"cloudflare_authenticated_origin_pulls_certificate": resourceCloudflareAuthenticatedOriginPullsCertificate(),
Expand Down
134 changes: 134 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_api_shield_schema.go
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.UpdateAPIShieldSchema(
ctx,
cloudflare.ZoneIdentifier(zoneID),
cloudflare.UpdateAPIShieldSchemaParams{
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
}
Loading
Loading