github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/scaleway/resource_scaleway_security_group.go (about)

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