github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_public_ip.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "regexp" 8 "strings" 9 10 "github.com/Azure/azure-sdk-for-go/arm/network" 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 func resourceArmPublicIp() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceArmPublicIpCreate, 17 Read: resourceArmPublicIpRead, 18 Update: resourceArmPublicIpCreate, 19 Delete: resourceArmPublicIpDelete, 20 Importer: &schema.ResourceImporter{ 21 State: schema.ImportStatePassthrough, 22 }, 23 24 Schema: map[string]*schema.Schema{ 25 "name": { 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 31 "location": { 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 StateFunc: azureRMNormalizeLocation, 36 }, 37 38 "resource_group_name": { 39 Type: schema.TypeString, 40 Required: true, 41 ForceNew: true, 42 }, 43 44 "public_ip_address_allocation": { 45 Type: schema.TypeString, 46 Required: true, 47 ValidateFunc: validatePublicIpAllocation, 48 StateFunc: func(val interface{}) string { 49 return strings.ToLower(val.(string)) 50 }, 51 }, 52 53 "idle_timeout_in_minutes": { 54 Type: schema.TypeInt, 55 Optional: true, 56 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 57 value := v.(int) 58 if value < 4 || value > 30 { 59 errors = append(errors, fmt.Errorf( 60 "The idle timeout must be between 4 and 30 minutes")) 61 } 62 return 63 }, 64 }, 65 66 "domain_name_label": { 67 Type: schema.TypeString, 68 Optional: true, 69 ValidateFunc: validatePublicIpDomainNameLabel, 70 }, 71 72 "reverse_fqdn": { 73 Type: schema.TypeString, 74 Optional: true, 75 }, 76 77 "fqdn": { 78 Type: schema.TypeString, 79 Computed: true, 80 }, 81 82 "ip_address": { 83 Type: schema.TypeString, 84 Computed: true, 85 }, 86 87 "tags": tagsSchema(), 88 }, 89 } 90 } 91 92 func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error { 93 client := meta.(*ArmClient) 94 publicIPClient := client.publicIPClient 95 96 log.Printf("[INFO] preparing arguments for Azure ARM Public IP creation.") 97 98 name := d.Get("name").(string) 99 location := d.Get("location").(string) 100 resGroup := d.Get("resource_group_name").(string) 101 tags := d.Get("tags").(map[string]interface{}) 102 103 properties := network.PublicIPAddressPropertiesFormat{ 104 PublicIPAllocationMethod: network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string)), 105 } 106 107 dnl, hasDnl := d.GetOk("domain_name_label") 108 rfqdn, hasRfqdn := d.GetOk("reverse_fqdn") 109 110 if hasDnl || hasRfqdn { 111 dnsSettings := network.PublicIPAddressDNSSettings{} 112 113 if hasRfqdn { 114 reverse_fqdn := rfqdn.(string) 115 dnsSettings.ReverseFqdn = &reverse_fqdn 116 } 117 118 if hasDnl { 119 domain_name_label := dnl.(string) 120 dnsSettings.DomainNameLabel = &domain_name_label 121 122 } 123 124 properties.DNSSettings = &dnsSettings 125 } 126 127 if v, ok := d.GetOk("idle_timeout_in_minutes"); ok { 128 idle_timeout := int32(v.(int)) 129 properties.IdleTimeoutInMinutes = &idle_timeout 130 } 131 132 publicIp := network.PublicIPAddress{ 133 Name: &name, 134 Location: &location, 135 Properties: &properties, 136 Tags: expandTags(tags), 137 } 138 139 _, err := publicIPClient.CreateOrUpdate(resGroup, name, publicIp, make(chan struct{})) 140 if err != nil { 141 return err 142 } 143 144 read, err := publicIPClient.Get(resGroup, name, "") 145 if err != nil { 146 return err 147 } 148 if read.ID == nil { 149 return fmt.Errorf("Cannot read Public IP %s (resource group %s) ID", name, resGroup) 150 } 151 152 d.SetId(*read.ID) 153 154 return resourceArmPublicIpRead(d, meta) 155 } 156 157 func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error { 158 publicIPClient := meta.(*ArmClient).publicIPClient 159 160 id, err := parseAzureResourceID(d.Id()) 161 if err != nil { 162 return err 163 } 164 resGroup := id.ResourceGroup 165 name := id.Path["publicIPAddresses"] 166 167 resp, err := publicIPClient.Get(resGroup, name, "") 168 if err != nil { 169 if resp.StatusCode == http.StatusNotFound { 170 d.SetId("") 171 return nil 172 } 173 return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err) 174 } 175 176 d.Set("resource_group_name", resGroup) 177 d.Set("location", resp.Location) 178 d.Set("name", resp.Name) 179 d.Set("public_ip_address_allocation", strings.ToLower(string(resp.Properties.PublicIPAllocationMethod))) 180 181 if resp.Properties.DNSSettings != nil && resp.Properties.DNSSettings.Fqdn != nil && *resp.Properties.DNSSettings.Fqdn != "" { 182 d.Set("fqdn", resp.Properties.DNSSettings.Fqdn) 183 } 184 185 if resp.Properties.IPAddress != nil && *resp.Properties.IPAddress != "" { 186 d.Set("ip_address", resp.Properties.IPAddress) 187 } 188 189 flattenAndSetTags(d, resp.Tags) 190 191 return nil 192 } 193 194 func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error { 195 publicIPClient := meta.(*ArmClient).publicIPClient 196 197 id, err := parseAzureResourceID(d.Id()) 198 if err != nil { 199 return err 200 } 201 resGroup := id.ResourceGroup 202 name := id.Path["publicIPAddresses"] 203 204 _, err = publicIPClient.Delete(resGroup, name, make(chan struct{})) 205 206 return err 207 } 208 209 func validatePublicIpAllocation(v interface{}, k string) (ws []string, errors []error) { 210 value := strings.ToLower(v.(string)) 211 allocations := map[string]bool{ 212 "static": true, 213 "dynamic": true, 214 } 215 216 if !allocations[value] { 217 errors = append(errors, fmt.Errorf("Public IP Allocation can only be Static of Dynamic")) 218 } 219 return 220 } 221 222 func validatePublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) { 223 value := v.(string) 224 if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) { 225 errors = append(errors, fmt.Errorf( 226 "only alphanumeric characters and hyphens allowed in %q: %q", 227 k, value)) 228 } 229 230 if len(value) > 61 { 231 errors = append(errors, fmt.Errorf( 232 "%q cannot be longer than 61 characters: %q", k, value)) 233 } 234 235 if len(value) == 0 { 236 errors = append(errors, fmt.Errorf( 237 "%q cannot be an empty string: %q", k, value)) 238 } 239 if regexp.MustCompile(`-$`).MatchString(value) { 240 errors = append(errors, fmt.Errorf( 241 "%q cannot end with a hyphen: %q", k, value)) 242 } 243 244 return 245 }