Skip to content

Commit

Permalink
APISHI-2358 resource/cloudflare_api_shield_schema: add API Shield Sch…
Browse files Browse the repository at this point in the history
…ema resource
  • Loading branch information
djhworld committed Oct 16, 2023
1 parent 14b2f89 commit 9d4a9af
Show file tree
Hide file tree
Showing 6 changed files with 485 additions and 0 deletions.
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")
}
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.PatchAPIShieldSchema(

Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / test

client.PatchAPIShieldSchema undefined (type *cloudflare.API has no field or method PatchAPIShieldSchema)

Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

client.PatchAPIShieldSchema undefined (type *cloudflare.API has no field or method PatchAPIShieldSchema)

Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

client.PatchAPIShieldSchema undefined (type *cloudflare.API has no field or method PatchAPIShieldSchema)

Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

client.PatchAPIShieldSchema undefined (type *cloudflare.API has no field or method PatchAPIShieldSchema)

Check failure on line 102 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

client.PatchAPIShieldSchema undefined (type *cloudflare.API has no field or method PatchAPIShieldSchema)
ctx,
cloudflare.ZoneIdentifier(zoneID),
cloudflare.PatchAPIShieldSchemaParams{

Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / test

undefined: cloudflare.PatchAPIShieldSchemaParams

Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

undefined: cloudflare.PatchAPIShieldSchemaParams

Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

undefined: cloudflare.PatchAPIShieldSchemaParams

Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

undefined: cloudflare.PatchAPIShieldSchemaParams

Check failure on line 105 in internal/sdkv2provider/resource_cloudflare_api_shield_schema.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

undefined: cloudflare.PatchAPIShieldSchemaParams
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

0 comments on commit 9d4a9af

Please sign in to comment.