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

Commit 975a2f7

Browse files
shelomentsevdalexk53
authored andcommitted
GCLOUD2-6855: Added DDoS protection support to provider
1 parent 45c8bbf commit 975a2f7

14 files changed

+1146
-31
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_ddos_profile_template Data Source - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represents list of available DDoS protection profile templates
7+
---
8+
9+
# gcore_ddos_profile_template (Data Source)
10+
11+
Represents list of available DDoS protection profile templates
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider gcore {
17+
permanent_api_token = "251$d3361.............1b35f26d8"
18+
}
19+
20+
data "gcore_project" "pr" {
21+
name = "test"
22+
}
23+
24+
data "gcore_region" "rg" {
25+
name = "ED-10 Preprod"
26+
}
27+
28+
data "gcore_ddos_profile_template" "template" {
29+
template_id = 63
30+
region_id = data.gcore_region.rg.id
31+
project_id = data.gcore_project.pr.id
32+
}
33+
34+
output "view" {
35+
value = data.gcore_ddos_profile_template.template
36+
}
37+
```
38+
39+
<!-- schema generated by tfplugindocs -->
40+
## Schema
41+
42+
### Optional
43+
44+
- `name` (String) Template name
45+
- `project_id` (Number)
46+
- `project_name` (String)
47+
- `region_id` (Number)
48+
- `region_name` (String)
49+
- `template_id` (Number) Template id
50+
51+
### Read-Only
52+
53+
- `description` (String) Template description
54+
- `fields` (List of Object) Additional fields (see [below for nested schema](#nestedatt--fields))
55+
- `id` (String) The ID of this resource.
56+
57+
<a id="nestedatt--fields"></a>
58+
### Nested Schema for `fields`
59+
60+
Read-Only:
61+
62+
- `default` (String)
63+
- `description` (String)
64+
- `field_type` (String)
65+
- `id` (Number)
66+
- `name` (String)
67+
- `required` (Boolean)
68+
- `validation_schema` (String)
69+
70+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_ddos_protection Resource - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represents DDoS protection profile
7+
---
8+
9+
# gcore_ddos_protection (Resource)
10+
11+
Represents DDoS protection profile
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `bm_instance_id` (String)
21+
- `ip_address` (String) IP address
22+
- `profile_template` (Number) Profile template ID
23+
24+
### Optional
25+
26+
- `active` (Boolean) Activate profile
27+
- `bgp` (Boolean) Activate BGP protocol
28+
- `fields` (Block List) (see [below for nested schema](#nestedblock--fields))
29+
- `last_updated` (String)
30+
- `project_id` (Number)
31+
- `project_name` (String)
32+
- `region_id` (Number)
33+
- `region_name` (String)
34+
35+
### Read-Only
36+
37+
- `id` (String) The ID of this resource.
38+
- `price` (String)
39+
- `protocols` (List of Object) List of protocols (see [below for nested schema](#nestedatt--protocols))
40+
- `site` (String)
41+
42+
<a id="nestedblock--fields"></a>
43+
### Nested Schema for `fields`
44+
45+
Required:
46+
47+
- `base_field` (Number)
48+
49+
Optional:
50+
51+
- `field_value` (String) Complex value. Only one of 'value' or 'field_value' must be specified.
52+
- `value` (String) Basic type value. Only one of 'value' or 'field_value' must be specified.
53+
54+
Read-Only:
55+
56+
- `default` (String)
57+
- `description` (String) Field description
58+
- `field_type` (String)
59+
- `id` (Number) The ID of this resource.
60+
- `name` (String)
61+
- `required` (Boolean)
62+
- `validation_schema` (String) Json schema to validate field_values
63+
64+
65+
<a id="nestedatt--protocols"></a>
66+
### Nested Schema for `protocols`
67+
68+
Read-Only:
69+
70+
- `port` (String)
71+
- `protocols` (List of String)
72+
73+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
provider gcore {
2+
permanent_api_token = "251$d3361.............1b35f26d8"
3+
}
4+
5+
data "gcore_project" "pr" {
6+
name = "test"
7+
}
8+
9+
data "gcore_region" "rg" {
10+
name = "ED-10 Preprod"
11+
}
12+
13+
data "gcore_ddos_profile_template" "template" {
14+
template_id = 63
15+
region_id = data.gcore_region.rg.id
16+
project_id = data.gcore_project.pr.id
17+
}
18+
19+
output "view" {
20+
value = data.gcore_ddos_profile_template.template
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# import using <project_id>:<region_id>:<profile_id> format
2+
terraform import gcore_ddos_protection.test 1:6:1337
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
provider gcore {
2+
permanent_api_token = "251$d3361.............1b35f26d8"
3+
}
4+
5+
resource "gcore_ddos_protection" "ddos_protection" {
6+
project_id = 1
7+
region_id = 1
8+
profile_template = 63
9+
ip_address = "10.94.77.72"
10+
bm_instance_id = "99cd3a2d-607f-4fbb-91d9-01fe926b1e7f"
11+
fields {
12+
base_field = 118
13+
field_value = [33033]
14+
}
15+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package gcore
2+
3+
import (
4+
"context"
5+
"log"
6+
"strconv"
7+
8+
"github.com/G-Core/gcorelabscloud-go/gcore/ddos/v1/ddos"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
)
12+
13+
const (
14+
ddosTemplatesPoint = "ddos/profile-templates"
15+
)
16+
17+
func dataSourceDDoSProfileTemplate() *schema.Resource {
18+
return &schema.Resource{
19+
ReadContext: dataSourceDDoSProfileTemplateRead,
20+
Description: "Represents list of available DDoS protection profile templates",
21+
Schema: map[string]*schema.Schema{
22+
"project_id": &schema.Schema{
23+
Type: schema.TypeInt,
24+
Optional: true,
25+
ForceNew: true,
26+
ExactlyOneOf: []string{
27+
"project_id",
28+
"project_name",
29+
},
30+
},
31+
"region_id": &schema.Schema{
32+
Type: schema.TypeInt,
33+
Optional: true,
34+
ForceNew: true,
35+
ExactlyOneOf: []string{
36+
"region_id",
37+
"region_name",
38+
},
39+
},
40+
"region_name": &schema.Schema{
41+
Type: schema.TypeString,
42+
Optional: true,
43+
ForceNew: true,
44+
ExactlyOneOf: []string{
45+
"region_id",
46+
"region_name",
47+
},
48+
},
49+
"project_name": &schema.Schema{
50+
Type: schema.TypeString,
51+
Optional: true,
52+
ForceNew: true,
53+
ExactlyOneOf: []string{
54+
"project_id",
55+
"project_name",
56+
},
57+
},
58+
"template_id": &schema.Schema{
59+
Type: schema.TypeInt,
60+
Optional: true,
61+
Computed: true,
62+
Description: "Template id",
63+
ExactlyOneOf: []string{
64+
"template_id",
65+
"name",
66+
},
67+
},
68+
"name": &schema.Schema{
69+
Type: schema.TypeString,
70+
Optional: true,
71+
Computed: true,
72+
Description: "Template name",
73+
ExactlyOneOf: []string{
74+
"template_id",
75+
"name",
76+
},
77+
},
78+
"fields": &schema.Schema{
79+
Type: schema.TypeList,
80+
Computed: true,
81+
Description: "Additional fields",
82+
Elem: &schema.Resource{
83+
Schema: map[string]*schema.Schema{
84+
"id": &schema.Schema{
85+
Type: schema.TypeInt,
86+
Computed: true,
87+
},
88+
"name": &schema.Schema{
89+
Type: schema.TypeString,
90+
Computed: true,
91+
Description: "Field name",
92+
},
93+
"field_type": &schema.Schema{
94+
Type: schema.TypeString,
95+
Computed: true,
96+
Description: "Field type",
97+
},
98+
"required": &schema.Schema{
99+
Type: schema.TypeBool,
100+
Computed: true,
101+
},
102+
"description": &schema.Schema{
103+
Type: schema.TypeString,
104+
Computed: true,
105+
Description: "Field description",
106+
},
107+
"default": &schema.Schema{
108+
Type: schema.TypeString,
109+
Computed: true,
110+
},
111+
"validation_schema": {
112+
Type: schema.TypeString,
113+
Computed: true,
114+
Description: "Json schema to validate field_values",
115+
},
116+
},
117+
},
118+
},
119+
"description": &schema.Schema{
120+
Type: schema.TypeString,
121+
Description: "Template description",
122+
Computed: true,
123+
},
124+
},
125+
}
126+
}
127+
128+
func dataSourceDDoSProfileTemplateRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
129+
log.Println("[DEBUG] Starts DDoS protection profile template reading")
130+
var diags diag.Diagnostics
131+
config := m.(*Config)
132+
provider := config.Provider
133+
134+
client, err := CreateClient(provider, d, ddosTemplatesPoint, versionPointV1)
135+
if err != nil {
136+
return diag.FromErr(err)
137+
}
138+
139+
id := d.Get("template_id").(int)
140+
name := d.Get("name").(string)
141+
templates, err := ddos.ListAllProfileTemplates(client)
142+
if err != nil {
143+
return diag.FromErr(err)
144+
}
145+
146+
var found bool
147+
var template ddos.ProfileTemplate
148+
for _, t := range templates {
149+
if t.ID == id {
150+
template = t
151+
found = true
152+
break
153+
}
154+
155+
if t.Name == name {
156+
template = t
157+
found = true
158+
break
159+
}
160+
}
161+
162+
if !found {
163+
return diag.Errorf("DDoS protection profile template not found not by ID %d nor by name %s", id, name)
164+
}
165+
166+
d.SetId(strconv.Itoa(template.ID))
167+
d.Set("name", template.Name)
168+
d.Set("description", template.Description)
169+
fields := make([]map[string]interface{}, len(template.Fields))
170+
for i, f := range template.Fields {
171+
field := map[string]interface{}{
172+
"field_type": f.FieldType,
173+
"required": f.Required,
174+
"id": f.ID,
175+
"default": f.Default,
176+
"description": f.Description,
177+
"name": f.Name,
178+
"validation_schema": string(f.ValidationSchema),
179+
}
180+
181+
fields[i] = field
182+
}
183+
if err := d.Set("fields", fields); err != nil {
184+
return diag.FromErr(err)
185+
}
186+
187+
log.Println("[DEBUG] Finish DDoS protection profile template reading")
188+
return diags
189+
}

0 commit comments

Comments
 (0)