Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
GCLOUD2-6855: Added DDoS protection support to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
shelomentsevd authored and alexk53 committed Dec 20, 2022
1 parent 45c8bbf commit 975a2f7
Show file tree
Hide file tree
Showing 14 changed files with 1,146 additions and 31 deletions.
70 changes: 70 additions & 0 deletions docs/data-sources/gcore_ddos_profile_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_ddos_profile_template Data Source - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represents list of available DDoS protection profile templates
---

# gcore_ddos_profile_template (Data Source)

Represents list of available DDoS protection profile templates

## Example Usage

```terraform
provider gcore {
permanent_api_token = "251$d3361.............1b35f26d8"
}
data "gcore_project" "pr" {
name = "test"
}
data "gcore_region" "rg" {
name = "ED-10 Preprod"
}
data "gcore_ddos_profile_template" "template" {
template_id = 63
region_id = data.gcore_region.rg.id
project_id = data.gcore_project.pr.id
}
output "view" {
value = data.gcore_ddos_profile_template.template
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `name` (String) Template name
- `project_id` (Number)
- `project_name` (String)
- `region_id` (Number)
- `region_name` (String)
- `template_id` (Number) Template id

### Read-Only

- `description` (String) Template description
- `fields` (List of Object) Additional fields (see [below for nested schema](#nestedatt--fields))
- `id` (String) The ID of this resource.

<a id="nestedatt--fields"></a>
### Nested Schema for `fields`

Read-Only:

- `default` (String)
- `description` (String)
- `field_type` (String)
- `id` (Number)
- `name` (String)
- `required` (Boolean)
- `validation_schema` (String)


73 changes: 73 additions & 0 deletions docs/resources/gcore_ddos_protection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_ddos_protection Resource - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represents DDoS protection profile
---

# gcore_ddos_protection (Resource)

Represents DDoS protection profile



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `bm_instance_id` (String)
- `ip_address` (String) IP address
- `profile_template` (Number) Profile template ID

### Optional

- `active` (Boolean) Activate profile
- `bgp` (Boolean) Activate BGP protocol
- `fields` (Block List) (see [below for nested schema](#nestedblock--fields))
- `last_updated` (String)
- `project_id` (Number)
- `project_name` (String)
- `region_id` (Number)
- `region_name` (String)

### Read-Only

- `id` (String) The ID of this resource.
- `price` (String)
- `protocols` (List of Object) List of protocols (see [below for nested schema](#nestedatt--protocols))
- `site` (String)

<a id="nestedblock--fields"></a>
### Nested Schema for `fields`

Required:

- `base_field` (Number)

Optional:

- `field_value` (String) Complex value. Only one of 'value' or 'field_value' must be specified.
- `value` (String) Basic type value. Only one of 'value' or 'field_value' must be specified.

Read-Only:

- `default` (String)
- `description` (String) Field description
- `field_type` (String)
- `id` (Number) The ID of this resource.
- `name` (String)
- `required` (Boolean)
- `validation_schema` (String) Json schema to validate field_values


<a id="nestedatt--protocols"></a>
### Nested Schema for `protocols`

Read-Only:

- `port` (String)
- `protocols` (List of String)


21 changes: 21 additions & 0 deletions examples/data-sources/gcore_ddos_profile_template/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
provider gcore {
permanent_api_token = "251$d3361.............1b35f26d8"
}

data "gcore_project" "pr" {
name = "test"
}

data "gcore_region" "rg" {
name = "ED-10 Preprod"
}

data "gcore_ddos_profile_template" "template" {
template_id = 63
region_id = data.gcore_region.rg.id
project_id = data.gcore_project.pr.id
}

output "view" {
value = data.gcore_ddos_profile_template.template
}
2 changes: 2 additions & 0 deletions examples/resources/gcore_ddos_profile/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# import using <project_id>:<region_id>:<profile_id> format
terraform import gcore_ddos_protection.test 1:6:1337
15 changes: 15 additions & 0 deletions examples/resources/gcore_ddos_profile/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
provider gcore {
permanent_api_token = "251$d3361.............1b35f26d8"
}

resource "gcore_ddos_protection" "ddos_protection" {
project_id = 1
region_id = 1
profile_template = 63
ip_address = "10.94.77.72"
bm_instance_id = "99cd3a2d-607f-4fbb-91d9-01fe926b1e7f"
fields {
base_field = 118
field_value = [33033]
}
}
189 changes: 189 additions & 0 deletions gcore/data_source_gcore_ddos_profile_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package gcore

import (
"context"
"log"
"strconv"

"github.com/G-Core/gcorelabscloud-go/gcore/ddos/v1/ddos"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
ddosTemplatesPoint = "ddos/profile-templates"
)

func dataSourceDDoSProfileTemplate() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDDoSProfileTemplateRead,
Description: "Represents list of available DDoS protection profile templates",
Schema: map[string]*schema.Schema{
"project_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{
"project_id",
"project_name",
},
},
"region_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{
"region_id",
"region_name",
},
},
"region_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{
"region_id",
"region_name",
},
},
"project_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{
"project_id",
"project_name",
},
},
"template_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Template id",
ExactlyOneOf: []string{
"template_id",
"name",
},
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Template name",
ExactlyOneOf: []string{
"template_id",
"name",
},
},
"fields": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Additional fields",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Field name",
},
"field_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Field type",
},
"required": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Field description",
},
"default": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"validation_schema": {
Type: schema.TypeString,
Computed: true,
Description: "Json schema to validate field_values",
},
},
},
},
"description": &schema.Schema{
Type: schema.TypeString,
Description: "Template description",
Computed: true,
},
},
}
}

func dataSourceDDoSProfileTemplateRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Starts DDoS protection profile template reading")
var diags diag.Diagnostics
config := m.(*Config)
provider := config.Provider

client, err := CreateClient(provider, d, ddosTemplatesPoint, versionPointV1)
if err != nil {
return diag.FromErr(err)
}

id := d.Get("template_id").(int)
name := d.Get("name").(string)
templates, err := ddos.ListAllProfileTemplates(client)
if err != nil {
return diag.FromErr(err)
}

var found bool
var template ddos.ProfileTemplate
for _, t := range templates {
if t.ID == id {
template = t
found = true
break
}

if t.Name == name {
template = t
found = true
break
}
}

if !found {
return diag.Errorf("DDoS protection profile template not found not by ID %d nor by name %s", id, name)
}

d.SetId(strconv.Itoa(template.ID))
d.Set("name", template.Name)
d.Set("description", template.Description)
fields := make([]map[string]interface{}, len(template.Fields))
for i, f := range template.Fields {
field := map[string]interface{}{
"field_type": f.FieldType,
"required": f.Required,
"id": f.ID,
"default": f.Default,
"description": f.Description,
"name": f.Name,
"validation_schema": string(f.ValidationSchema),
}

fields[i] = field
}
if err := d.Set("fields", fields); err != nil {
return diag.FromErr(err)
}

log.Println("[DEBUG] Finish DDoS protection profile template reading")
return diags
}
Loading

0 comments on commit 975a2f7

Please sign in to comment.