github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  			"description": {
    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  	mu.Lock()
    38  	defer mu.Unlock()
    39  
    40  	req := api.ScalewayNewSecurityGroup{
    41  		Name:         d.Get("name").(string),
    42  		Description:  d.Get("description").(string),
    43  		Organization: scaleway.Organization,
    44  	}
    45  
    46  	err := scaleway.PostSecurityGroup(req)
    47  	if err != nil {
    48  		if serr, ok := err.(api.ScalewayAPIError); ok {
    49  			log.Printf("[DEBUG] Error creating security group: %q\n", serr.APIMessage)
    50  		}
    51  
    52  		return err
    53  	}
    54  
    55  	resp, err := scaleway.GetSecurityGroups()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	for _, group := range resp.SecurityGroups {
    61  		if group.Name == req.Name {
    62  			d.SetId(group.ID)
    63  			break
    64  		}
    65  	}
    66  
    67  	if d.Id() == "" {
    68  		return fmt.Errorf("Failed to find created security group.")
    69  	}
    70  
    71  	return resourceScalewaySecurityGroupRead(d, m)
    72  }
    73  
    74  func resourceScalewaySecurityGroupRead(d *schema.ResourceData, m interface{}) error {
    75  	scaleway := m.(*Client).scaleway
    76  	resp, err := scaleway.GetASecurityGroup(d.Id())
    77  
    78  	if err != nil {
    79  		if serr, ok := err.(api.ScalewayAPIError); ok {
    80  			log.Printf("[DEBUG] Error reading security group: %q\n", serr.APIMessage)
    81  
    82  			if serr.StatusCode == 404 {
    83  				d.SetId("")
    84  				return nil
    85  			}
    86  		}
    87  
    88  		return err
    89  	}
    90  
    91  	d.Set("name", resp.SecurityGroups.Name)
    92  	d.Set("description", resp.SecurityGroups.Description)
    93  
    94  	return nil
    95  }
    96  
    97  func resourceScalewaySecurityGroupUpdate(d *schema.ResourceData, m interface{}) error {
    98  	scaleway := m.(*Client).scaleway
    99  
   100  	mu.Lock()
   101  	defer mu.Unlock()
   102  
   103  	var req = api.ScalewayUpdateSecurityGroup{
   104  		Organization: scaleway.Organization,
   105  		Name:         d.Get("name").(string),
   106  		Description:  d.Get("description").(string),
   107  	}
   108  
   109  	if err := scaleway.PutSecurityGroup(req, d.Id()); err != nil {
   110  		log.Printf("[DEBUG] Error reading security group: %q\n", err)
   111  
   112  		return err
   113  	}
   114  
   115  	return resourceScalewaySecurityGroupRead(d, m)
   116  }
   117  
   118  func resourceScalewaySecurityGroupDelete(d *schema.ResourceData, m interface{}) error {
   119  	scaleway := m.(*Client).scaleway
   120  
   121  	mu.Lock()
   122  	defer mu.Unlock()
   123  
   124  	err := scaleway.DeleteSecurityGroup(d.Id())
   125  	if err != nil {
   126  		if serr, ok := err.(api.ScalewayAPIError); ok {
   127  			log.Printf("[DEBUG] error reading Security Group Rule: %q\n", serr.APIMessage)
   128  
   129  			if serr.StatusCode == 404 {
   130  				d.SetId("")
   131  				return nil
   132  			}
   133  		}
   134  
   135  		return err
   136  	}
   137  
   138  	d.SetId("")
   139  	return nil
   140  }