2015-04-30 10:58:45 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/networksecuritygroup"
|
2015-04-30 10:58:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAzureSecurityGroupCreate,
|
|
|
|
Read: resourceAzureSecurityGroupRead,
|
|
|
|
Delete: resourceAzureSecurityGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"label": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"location": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
2015-05-29 00:10:21 +02:00
|
|
|
mc := meta.(*Client).mgmtClient
|
2015-04-30 10:58:45 +02:00
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
2015-05-18 15:40:45 +02:00
|
|
|
// Compute/set the label
|
|
|
|
label := d.Get("label").(string)
|
|
|
|
if label == "" {
|
|
|
|
label = name
|
|
|
|
}
|
|
|
|
|
2015-05-22 22:02:48 +02:00
|
|
|
req, err := networksecuritygroup.NewClient(mc).CreateNetworkSecurityGroup(
|
2015-04-30 10:58:45 +02:00
|
|
|
name,
|
2015-05-18 15:40:45 +02:00
|
|
|
label,
|
2015-04-30 10:58:45 +02:00
|
|
|
d.Get("location").(string),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Network Security Group %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:31:14 +02:00
|
|
|
if err := mc.WaitForOperation(req, nil); err != nil {
|
2015-04-30 10:58:45 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Network Security Group %s to be created: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(name)
|
|
|
|
|
|
|
|
return resourceAzureSecurityGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
|
2015-05-29 00:10:21 +02:00
|
|
|
mc := meta.(*Client).mgmtClient
|
2015-04-30 10:58:45 +02:00
|
|
|
|
2015-05-22 22:02:48 +02:00
|
|
|
sg, err := networksecuritygroup.NewClient(mc).GetNetworkSecurityGroup(d.Id())
|
2015-04-30 10:58:45 +02:00
|
|
|
if err != nil {
|
2015-06-01 11:18:48 +02:00
|
|
|
if management.IsResourceNotFoundError(err) {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-30 10:58:45 +02:00
|
|
|
return fmt.Errorf("Error retrieving Network Security Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("label", sg.Label)
|
|
|
|
d.Set("location", sg.Location)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-05-29 00:10:21 +02:00
|
|
|
mc := meta.(*Client).mgmtClient
|
2015-04-30 10:58:45 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Deleting Network Security Group: %s", d.Id())
|
2015-05-22 22:02:48 +02:00
|
|
|
req, err := networksecuritygroup.NewClient(mc).DeleteNetworkSecurityGroup(d.Id())
|
2015-04-30 10:58:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Network Security Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the network security group is deleted
|
2015-05-22 15:31:14 +02:00
|
|
|
if err := mc.WaitForOperation(req, nil); err != nil {
|
2015-04-30 10:58:45 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for Network Security Group %s to be deleted: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|