github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_container_registry.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"net/http"
     8  
     9  	"regexp"
    10  
    11  	"github.com/Azure/azure-sdk-for-go/arm/containerregistry"
    12  	"github.com/hashicorp/terraform/helper/hashcode"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  	"github.com/jen20/riviera/azure"
    15  )
    16  
    17  func resourceArmContainerRegistry() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceArmContainerRegistryCreate,
    20  		Read:   resourceArmContainerRegistryRead,
    21  		Update: resourceArmContainerRegistryCreate,
    22  		Delete: resourceArmContainerRegistryDelete,
    23  		Importer: &schema.ResourceImporter{
    24  			State: schema.ImportStatePassthrough,
    25  		},
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"name": {
    29  				Type:         schema.TypeString,
    30  				Required:     true,
    31  				ForceNew:     true,
    32  				ValidateFunc: validateAzureRMContainerRegistryName,
    33  			},
    34  
    35  			"resource_group_name": {
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"location": locationSchema(),
    42  
    43  			"admin_enabled": {
    44  				Type:     schema.TypeBool,
    45  				Optional: true,
    46  				Default:  false,
    47  			},
    48  
    49  			"storage_account": {
    50  				Type:     schema.TypeSet,
    51  				Required: true,
    52  				MaxItems: 1,
    53  				Elem: &schema.Resource{
    54  					Schema: map[string]*schema.Schema{
    55  						"name": {
    56  							Type:     schema.TypeString,
    57  							Required: true,
    58  						},
    59  
    60  						"access_key": {
    61  							Type:      schema.TypeString,
    62  							Required:  true,
    63  							Sensitive: true,
    64  						},
    65  					},
    66  				},
    67  			},
    68  
    69  			"login_server": {
    70  				Type:     schema.TypeString,
    71  				Computed: true,
    72  			},
    73  
    74  			"admin_username": {
    75  				Type:     schema.TypeString,
    76  				Computed: true,
    77  			},
    78  
    79  			"admin_password": {
    80  				Type:     schema.TypeString,
    81  				Computed: true,
    82  			},
    83  
    84  			"tags": tagsSchema(),
    85  		},
    86  	}
    87  }
    88  
    89  func resourceArmContainerRegistryCreate(d *schema.ResourceData, meta interface{}) error {
    90  	client := meta.(*ArmClient).containerRegistryClient
    91  	log.Printf("[INFO] preparing arguments for AzureRM Container Registry creation.")
    92  
    93  	resourceGroup := d.Get("resource_group_name").(string)
    94  	name := d.Get("name").(string)
    95  	location := d.Get("location").(string)
    96  
    97  	adminUserEnabled := d.Get("admin_enabled").(bool)
    98  	tags := d.Get("tags").(map[string]interface{})
    99  
   100  	parameters := containerregistry.Registry{
   101  		Location: &location,
   102  		RegistryProperties: &containerregistry.RegistryProperties{
   103  			AdminUserEnabled: &adminUserEnabled,
   104  		},
   105  		Tags: expandTags(tags),
   106  	}
   107  
   108  	accounts := d.Get("storage_account").(*schema.Set).List()
   109  	account := accounts[0].(map[string]interface{})
   110  	storageAccountName := account["name"].(string)
   111  	storageAccountAccessKey := account["access_key"].(string)
   112  	parameters.RegistryProperties.StorageAccount = &containerregistry.StorageAccountProperties{
   113  		Name:      azure.String(storageAccountName),
   114  		AccessKey: azure.String(storageAccountAccessKey),
   115  	}
   116  
   117  	_, err := client.CreateOrUpdate(resourceGroup, name, parameters)
   118  	if err != nil {
   119  		return err
   120  	}
   121  
   122  	read, err := client.GetProperties(resourceGroup, name)
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	if read.ID == nil {
   128  		return fmt.Errorf("Cannot read Container Registry %s (resource group %s) ID", name, resourceGroup)
   129  	}
   130  
   131  	d.SetId(*read.ID)
   132  
   133  	return resourceArmContainerRegistryRead(d, meta)
   134  }
   135  
   136  func resourceArmContainerRegistryRead(d *schema.ResourceData, meta interface{}) error {
   137  	client := meta.(*ArmClient).containerRegistryClient
   138  
   139  	id, err := parseAzureResourceID(d.Id())
   140  	if err != nil {
   141  		return err
   142  	}
   143  	resourceGroup := id.ResourceGroup
   144  	name := id.Path["registries"]
   145  
   146  	resp, err := client.GetProperties(resourceGroup, name)
   147  	if err != nil {
   148  		return fmt.Errorf("Error making Read request on Azure Container Registry %s: %s", name, err)
   149  	}
   150  	if resp.StatusCode == http.StatusNotFound {
   151  		d.SetId("")
   152  		return nil
   153  	}
   154  
   155  	d.Set("name", resp.Name)
   156  	d.Set("resource_group_name", resourceGroup)
   157  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   158  	d.Set("admin_enabled", resp.AdminUserEnabled)
   159  	d.Set("login_server", resp.LoginServer)
   160  
   161  	if resp.StorageAccount != nil {
   162  		flattenArmContainerRegistryStorageAccount(d, resp.StorageAccount)
   163  	}
   164  
   165  	if *resp.AdminUserEnabled {
   166  		credsResp, err := client.GetCredentials(resourceGroup, name)
   167  		if err != nil {
   168  			return fmt.Errorf("Error making Read request on Azure Container Registry %s for Credentials: %s", name, err)
   169  		}
   170  
   171  		d.Set("admin_username", credsResp.Username)
   172  		d.Set("admin_password", credsResp.Password)
   173  	} else {
   174  		d.Set("admin_username", "")
   175  		d.Set("admin_password", "")
   176  	}
   177  
   178  	flattenAndSetTags(d, resp.Tags)
   179  
   180  	return nil
   181  }
   182  
   183  func resourceArmContainerRegistryDelete(d *schema.ResourceData, meta interface{}) error {
   184  	client := meta.(*ArmClient).containerRegistryClient
   185  
   186  	id, err := parseAzureResourceID(d.Id())
   187  	if err != nil {
   188  		return err
   189  	}
   190  	resourceGroup := id.ResourceGroup
   191  	name := id.Path["registries"]
   192  
   193  	resp, err := client.Delete(resourceGroup, name)
   194  
   195  	if resp.StatusCode != http.StatusOK {
   196  		return fmt.Errorf("Error issuing Azure ARM delete request of Container Registry '%s': %s", name, err)
   197  	}
   198  
   199  	return nil
   200  }
   201  
   202  func flattenArmContainerRegistryStorageAccount(d *schema.ResourceData, properties *containerregistry.StorageAccountProperties) {
   203  	storageAccounts := schema.Set{
   204  		F: resourceAzureRMContainerRegistryStorageAccountHash,
   205  	}
   206  
   207  	storageAccount := map[string]interface{}{}
   208  	storageAccount["name"] = properties.Name
   209  	storageAccounts.Add(storageAccount)
   210  
   211  	d.Set("storage_account", &storageAccounts)
   212  }
   213  
   214  func resourceAzureRMContainerRegistryStorageAccountHash(v interface{}) int {
   215  	m := v.(map[string]interface{})
   216  	name := m["name"].(*string)
   217  	return hashcode.String(*name)
   218  }
   219  
   220  func validateAzureRMContainerRegistryName(v interface{}, k string) (ws []string, errors []error) {
   221  	value := v.(string)
   222  	if !regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(value) {
   223  		errors = append(errors, fmt.Errorf(
   224  			"alpha numeric characters only are allowed in %q: %q", k, value))
   225  	}
   226  
   227  	if 5 > len(value) {
   228  		errors = append(errors, fmt.Errorf("%q cannot be less than 5 characters: %q", k, value))
   229  	}
   230  
   231  	if len(value) >= 50 {
   232  		errors = append(errors, fmt.Errorf("%q cannot be longer than 50 characters: %q %d", k, value, len(value)))
   233  	}
   234  
   235  	return
   236  }