2015-04-30 10:58:45 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2015-05-18 15:40:45 +02:00
|
|
|
"bytes"
|
2015-04-30 10:58:45 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
2015-05-18 15:40:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2015-04-30 10:58:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-05-12 08:30:47 +02:00
|
|
|
"github.com/svanharmelen/azure-sdk-for-go/management"
|
|
|
|
"github.com/svanharmelen/azure-sdk-for-go/management/networksecuritygroup"
|
2015-04-30 10:58:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAzureSecurityGroupCreate,
|
|
|
|
Read: resourceAzureSecurityGroupRead,
|
|
|
|
Update: resourceAzureSecurityGroupUpdate,
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
|
|
|
"subnet": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"location": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"rule": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-04-30 17:12:05 +02:00
|
|
|
Default: "Inbound",
|
2015-04-30 10:58:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"priority": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"action": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-04-30 17:12:05 +02:00
|
|
|
Default: "Allow",
|
2015-04-30 10:58:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"source_cidr": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"source_port": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"destination_cidr": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"destination_port": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-04-30 17:12:05 +02:00
|
|
|
Default: "TCP",
|
2015-04-30 10:58:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAzureSecurityGroupRuleHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
|
|
|
mc := meta.(*management.Client)
|
|
|
|
|
|
|
|
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-04-30 10:58:45 +02:00
|
|
|
req, err := networksecuritygroup.NewClient(*mc).CreateNetworkSecurityGroup(
|
|
|
|
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)
|
|
|
|
|
|
|
|
// Create all rules that are configured
|
|
|
|
if rs := d.Get("rule").(*schema.Set); rs.Len() > 0 {
|
|
|
|
|
|
|
|
// Create an empty schema.Set to hold all rules
|
|
|
|
rules := &schema.Set{
|
|
|
|
F: resourceAzureSecurityGroupRuleHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range rs.List() {
|
|
|
|
// Create a single rule
|
|
|
|
err := resourceAzureSecurityGroupRuleCreate(d, meta, rule.(map[string]interface{}))
|
|
|
|
|
|
|
|
// We need to update this first to preserve the correct state
|
|
|
|
rules.Add(rule)
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAzureSecurityGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupRuleCreate(
|
|
|
|
d *schema.ResourceData, meta interface{}, rule map[string]interface{}) error {
|
|
|
|
mc := meta.(*management.Client)
|
|
|
|
|
|
|
|
// Make sure all required parameters are there
|
|
|
|
if err := verifySecurityGroupRuleParams(rule); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rule["name"].(string)
|
|
|
|
|
|
|
|
// Create the rule
|
|
|
|
req, err := networksecuritygroup.NewClient(*mc).SetNetworkSecurityGroupRule(d.Id(),
|
2015-05-11 18:32:30 +02:00
|
|
|
networksecuritygroup.RuleRequest{
|
2015-04-30 10:58:45 +02:00
|
|
|
Name: name,
|
2015-05-11 18:32:30 +02:00
|
|
|
Type: networksecuritygroup.RuleType(rule["type"].(string)),
|
2015-04-30 10:58:45 +02:00
|
|
|
Priority: rule["priority"].(int),
|
2015-05-11 18:32:30 +02:00
|
|
|
Action: networksecuritygroup.RuleAction(rule["action"].(string)),
|
2015-04-30 10:58:45 +02:00
|
|
|
SourceAddressPrefix: rule["source_cidr"].(string),
|
|
|
|
SourcePortRange: rule["source_port"].(string),
|
|
|
|
DestinationAddressPrefix: rule["destination_cidr"].(string),
|
|
|
|
DestinationPortRange: rule["destination_port"].(string),
|
2015-05-11 18:32:30 +02:00
|
|
|
Protocol: networksecuritygroup.RuleProtocol(rule["protocol"].(string)),
|
2015-04-30 10:58:45 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Network Security Group rule %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 rule %s to be created: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
mc := meta.(*management.Client)
|
|
|
|
|
|
|
|
sg, err := networksecuritygroup.NewClient(*mc).GetNetworkSecurityGroup(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving Network Security Group %s: %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("label", sg.Label)
|
|
|
|
d.Set("location", sg.Location)
|
|
|
|
|
2015-04-30 17:12:05 +02:00
|
|
|
// Create an empty schema.Set to hold all rules
|
|
|
|
rules := &schema.Set{
|
|
|
|
F: resourceAzureSecurityGroupRuleHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range sg.Rules {
|
|
|
|
if !r.IsDefault {
|
|
|
|
rule := map[string]interface{}{
|
|
|
|
"name": r.Name,
|
2015-05-11 18:32:30 +02:00
|
|
|
"type": string(r.Type),
|
2015-04-30 17:12:05 +02:00
|
|
|
"priority": r.Priority,
|
2015-05-11 18:32:30 +02:00
|
|
|
"action": string(r.Action),
|
2015-04-30 17:12:05 +02:00
|
|
|
"source_cidr": r.SourceAddressPrefix,
|
|
|
|
"source_port": r.SourcePortRange,
|
|
|
|
"destination_cidr": r.DestinationAddressPrefix,
|
|
|
|
"destination_port": r.DestinationPortRange,
|
2015-05-11 18:32:30 +02:00
|
|
|
"protocol": string(r.Protocol),
|
2015-04-30 17:12:05 +02:00
|
|
|
}
|
|
|
|
rules.Add(rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
2015-04-30 10:58:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
// Check if the rule set as a whole has changed
|
|
|
|
if d.HasChange("rule") {
|
|
|
|
o, n := d.GetChange("rule")
|
|
|
|
ors := o.(*schema.Set).Difference(n.(*schema.Set))
|
|
|
|
nrs := n.(*schema.Set).Difference(o.(*schema.Set))
|
|
|
|
|
|
|
|
// Now first loop through all the old rules and delete any obsolete ones
|
|
|
|
for _, rule := range ors.List() {
|
|
|
|
// Delete the rule as it no longer exists in the config
|
|
|
|
err := resourceAzureSecurityGroupRuleDelete(d, meta, rule.(map[string]interface{}))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we save the state of the currently configured rules
|
|
|
|
rules := o.(*schema.Set).Intersection(n.(*schema.Set))
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
|
|
|
// Then loop through al the currently configured rules and create the new ones
|
|
|
|
for _, rule := range nrs.List() {
|
|
|
|
err := resourceAzureSecurityGroupRuleCreate(d, meta, rule.(map[string]interface{}))
|
|
|
|
|
|
|
|
// We need to update this first to preserve the correct state
|
|
|
|
rules.Add(rule)
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAzureSecurityGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
mc := meta.(*management.Client)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Deleting Network Security Group: %s", d.Id())
|
|
|
|
req, err := networksecuritygroup.NewClient(*mc).DeleteNetworkSecurityGroup(d.Id())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupRuleDelete(
|
|
|
|
d *schema.ResourceData, meta interface{}, rule map[string]interface{}) error {
|
|
|
|
mc := meta.(*management.Client)
|
|
|
|
|
|
|
|
name := rule["name"].(string)
|
|
|
|
|
|
|
|
// Delete the rule
|
|
|
|
req, err := networksecuritygroup.NewClient(*mc).DeleteNetworkSecurityGroupRule(d.Id(), name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Network Security Group rule %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 rule %s to be deleted: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureSecurityGroupRuleHash(v interface{}) int {
|
2015-05-18 15:40:45 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf(
|
|
|
|
"%s-%d-%s-%s-%s-%s-%s-%s",
|
|
|
|
m["type"].(string),
|
|
|
|
m["priority"].(int),
|
|
|
|
m["action"].(string),
|
|
|
|
m["source_cidr"].(string),
|
|
|
|
m["source_port"].(string),
|
|
|
|
m["destination_cidr"].(string),
|
|
|
|
m["destination_port"].(string),
|
|
|
|
m["protocol"].(string)))
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifySecurityGroupRuleParams(rule map[string]interface{}) error {
|
|
|
|
typ := rule["type"].(string)
|
2015-04-30 17:12:05 +02:00
|
|
|
if typ != "Inbound" && typ != "Outbound" {
|
|
|
|
return fmt.Errorf("Parameter type only accepts 'Inbound' or 'Outbound' as values")
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
action := rule["action"].(string)
|
2015-04-30 17:12:05 +02:00
|
|
|
if action != "Allow" && action != "Deny" {
|
|
|
|
return fmt.Errorf("Parameter action only accepts 'Allow' or 'Deny' as values")
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protocol := rule["protocol"].(string)
|
2015-04-30 17:12:05 +02:00
|
|
|
if protocol != "TCP" && protocol != "UDP" && protocol != "*" {
|
2015-04-30 10:58:45 +02:00
|
|
|
_, err := strconv.ParseInt(protocol, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
2015-04-30 17:12:05 +02:00
|
|
|
"Parameter type only accepts 'TCP', 'UDP' or '*' as values")
|
2015-04-30 10:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|