github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/azurerm/resource_arm_cdn_profile.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/Azure/azure-sdk-for-go/arm/cdn"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceArmCdnProfile() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmCdnProfileCreate,
    16  		Read:   resourceArmCdnProfileRead,
    17  		Update: resourceArmCdnProfileUpdate,
    18  		Delete: resourceArmCdnProfileDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": {
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"location": {
    28  				Type:      schema.TypeString,
    29  				Required:  true,
    30  				ForceNew:  true,
    31  				StateFunc: azureRMNormalizeLocation,
    32  			},
    33  
    34  			"resource_group_name": {
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"sku": {
    41  				Type:         schema.TypeString,
    42  				Required:     true,
    43  				ForceNew:     true,
    44  				ValidateFunc: validateCdnProfileSku,
    45  			},
    46  
    47  			"tags": tagsSchema(),
    48  		},
    49  	}
    50  }
    51  
    52  func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error {
    53  	client := meta.(*ArmClient)
    54  	cdnProfilesClient := client.cdnProfilesClient
    55  
    56  	log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")
    57  
    58  	name := d.Get("name").(string)
    59  	location := d.Get("location").(string)
    60  	resGroup := d.Get("resource_group_name").(string)
    61  	sku := d.Get("sku").(string)
    62  	tags := d.Get("tags").(map[string]interface{})
    63  
    64  	properties := cdn.ProfilePropertiesCreateParameters{
    65  		Sku: &cdn.Sku{
    66  			Name: cdn.SkuName(sku),
    67  		},
    68  	}
    69  
    70  	cdnProfile := cdn.ProfileCreateParameters{
    71  		Location:   &location,
    72  		Properties: &properties,
    73  		Tags:       expandTags(tags),
    74  	}
    75  
    76  	_, err := cdnProfilesClient.Create(name, cdnProfile, resGroup, make(chan struct{}))
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	read, err := cdnProfilesClient.Get(name, resGroup)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	if read.ID == nil {
    86  		return fmt.Errorf("Cannot read CND Profile %s (resource group %s) ID", name, resGroup)
    87  	}
    88  
    89  	d.SetId(*read.ID)
    90  
    91  	return resourceArmCdnProfileRead(d, meta)
    92  }
    93  
    94  func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
    95  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
    96  
    97  	id, err := parseAzureResourceID(d.Id())
    98  	if err != nil {
    99  		return err
   100  	}
   101  	resGroup := id.ResourceGroup
   102  	name := id.Path["Profiles"]
   103  
   104  	resp, err := cdnProfilesClient.Get(name, resGroup)
   105  	if resp.StatusCode == http.StatusNotFound {
   106  		d.SetId("")
   107  		return nil
   108  	}
   109  	if err != nil {
   110  		return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
   111  	}
   112  
   113  	if resp.Properties != nil && resp.Properties.Sku != nil {
   114  		d.Set("sku", string(resp.Properties.Sku.Name))
   115  	}
   116  
   117  	flattenAndSetTags(d, resp.Tags)
   118  
   119  	return nil
   120  }
   121  
   122  func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error {
   123  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
   124  
   125  	if !d.HasChange("tags") {
   126  		return nil
   127  	}
   128  
   129  	name := d.Get("name").(string)
   130  	resGroup := d.Get("resource_group_name").(string)
   131  	newTags := d.Get("tags").(map[string]interface{})
   132  
   133  	props := cdn.ProfileUpdateParameters{
   134  		Tags: expandTags(newTags),
   135  	}
   136  
   137  	_, err := cdnProfilesClient.Update(name, props, resGroup)
   138  	if err != nil {
   139  		return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err)
   140  	}
   141  
   142  	return resourceArmCdnProfileRead(d, meta)
   143  }
   144  
   145  func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error {
   146  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
   147  
   148  	id, err := parseAzureResourceID(d.Id())
   149  	if err != nil {
   150  		return err
   151  	}
   152  	resGroup := id.ResourceGroup
   153  	name := id.Path["Profiles"]
   154  
   155  	_, err = cdnProfilesClient.DeleteIfExists(name, resGroup, make(chan struct{}))
   156  
   157  	return err
   158  }
   159  
   160  func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) {
   161  	value := strings.ToLower(v.(string))
   162  	skus := map[string]bool{
   163  		"standard": true,
   164  		"premium":  true,
   165  	}
   166  
   167  	if !skus[value] {
   168  		errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Standard or Premium"))
   169  	}
   170  	return
   171  }