forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_scaleway_security_group.go
127 lines (100 loc) · 2.83 KB
/
resource_scaleway_security_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package scaleway
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/scaleway/scaleway-cli/pkg/api"
)
func resourceScalewaySecurityGroup() *schema.Resource {
return &schema.Resource{
Create: resourceScalewaySecurityGroupCreate,
Read: resourceScalewaySecurityGroupRead,
Update: resourceScalewaySecurityGroupUpdate,
Delete: resourceScalewaySecurityGroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceScalewaySecurityGroupCreate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
req := api.ScalewayNewSecurityGroup{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Organization: scaleway.Organization,
}
err := scaleway.PostSecurityGroup(req)
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
log.Printf("[DEBUG] Error creating security group: %q\n", serr.APIMessage)
}
return err
}
resp, err := scaleway.GetSecurityGroups()
if err != nil {
return err
}
for _, group := range resp.SecurityGroups {
if group.Name == req.Name {
d.SetId(group.ID)
break
}
}
if d.Id() == "" {
return fmt.Errorf("Failed to find created security group.")
}
return resourceScalewaySecurityGroupRead(d, m)
}
func resourceScalewaySecurityGroupRead(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
resp, err := scaleway.GetASecurityGroup(d.Id())
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
log.Printf("[DEBUG] Error reading security group: %q\n", serr.APIMessage)
if serr.StatusCode == 404 {
d.SetId("")
return nil
}
}
return err
}
d.Set("name", resp.SecurityGroups.Name)
d.Set("description", resp.SecurityGroups.Description)
return nil
}
func resourceScalewaySecurityGroupUpdate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
var req = api.ScalewayUpdateSecurityGroup{
Organization: scaleway.Organization,
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
if err := scaleway.PutSecurityGroup(req, d.Id()); err != nil {
log.Printf("[DEBUG] Error reading security group: %q\n", err)
return err
}
return resourceScalewaySecurityGroupRead(d, m)
}
func resourceScalewaySecurityGroupDelete(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
err := scaleway.DeleteSecurityGroup(d.Id())
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
log.Printf("[DEBUG] error reading Security Group Rule: %q\n", serr.APIMessage)
if serr.StatusCode == 404 {
d.SetId("")
return nil
}
}
return err
}
d.SetId("")
return nil
}