terraform/builtin/providers/scaleway/resource_scaleway_security_...

119 lines
2.6 KiB
Go
Raw Normal View History

Add scaleway provider (#7331) * Add scaleway provider this PR allows the entire scaleway stack to be managed with terraform example usage looks like this: ``` provider "scaleway" { api_key = "snap" organization = "snip" } resource "scaleway_ip" "base" { server = "${scaleway_server.base.id}" } resource "scaleway_server" "base" { name = "test" # ubuntu 14.04 image = "aecaed73-51a5-4439-a127-6d8229847145" type = "C2S" } resource "scaleway_volume" "test" { name = "test" size_in_gb = 20 type = "l_ssd" } resource "scaleway_volume_attachment" "test" { server = "${scaleway_server.base.id}" volume = "${scaleway_volume.test.id}" } resource "scaleway_security_group" "base" { name = "public" description = "public gateway" } resource "scaleway_security_group_rule" "http-ingress" { security_group = "${scaleway_security_group.base.id}" action = "accept" direction = "inbound" ip_range = "0.0.0.0/0" protocol = "TCP" port = 80 } resource "scaleway_security_group_rule" "http-egress" { security_group = "${scaleway_security_group.base.id}" action = "accept" direction = "outbound" ip_range = "0.0.0.0/0" protocol = "TCP" port = 80 } ``` Note that volume attachments require the server to be stopped, which can lead to downtimes of you attach new volumes to already used servers * Update IP read to handle 404 gracefully * Read back resource on update * Ensure IP detachment works as expected Sadly this is not part of the official scaleway api just yet * Adjust detachIP helper based on feedback from @QuentinPerez in https://github.com/scaleway/scaleway-cli/pull/378 * Cleanup documentation * Rename api_key to access_key following @stack72 suggestion and rename the provider api_key for more clarity * Make tests less chatty by using custom logger
2016-07-13 22:03:41 +02:00
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.ScalewayNewSecurityGroup{
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 {
return err
}
d.SetId("")
return nil
}