github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err) 170 } 171 if resp.StatusCode == http.StatusNotFound { 172 d.SetId("") 173 return nil 174 } 175 176 d.Set("location", resp.Location) 177 d.Set("name", resp.Name) 178 d.Set("public_ip_address_allocation", strings.ToLower(string(resp.Properties.PublicIPAllocationMethod))) 179 180 if resp.Properties.DNSSettings != nil && resp.Properties.DNSSettings.Fqdn != nil && *resp.Properties.DNSSettings.Fqdn != "" { 181 d.Set("fqdn", resp.Properties.DNSSettings.Fqdn) 182 } 183 184 if resp.Properties.IPAddress != nil && *resp.Properties.IPAddress != "" { 185 d.Set("ip_address", resp.Properties.IPAddress) 186 } 187 188 flattenAndSetTags(d, resp.Tags) 189 190 return nil 191 } 192 193 func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error { 194 publicIPClient := meta.(*ArmClient).publicIPClient 195 196 id, err := parseAzureResourceID(d.Id()) 197 if err != nil { 198 return err 199 } 200 resGroup := id.ResourceGroup 201 name := id.Path["publicIPAddresses"] 202 203 _, err = publicIPClient.Delete(resGroup, name, make(chan struct{})) 204 205 return err 206 } 207 208 func validatePublicIpAllocation(v interface{}, k string) (ws []string, errors []error) { 209 value := strings.ToLower(v.(string)) 210 allocations := map[string]bool{ 211 "static": true, 212 "dynamic": true, 213 } 214 215 if !allocations[value] { 216 errors = append(errors, fmt.Errorf("Public IP Allocation can only be Static of Dynamic")) 217 } 218 return 219 } 220 221 func validatePublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) { 222 value := v.(string) 223 if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) { 224 errors = append(errors, fmt.Errorf( 225 "only alphanumeric characters and hyphens allowed in %q: %q", 226 k, value)) 227 } 228 229 if len(value) > 61 { 230 errors = append(errors, fmt.Errorf( 231 "%q cannot be longer than 61 characters: %q", k, value)) 232 } 233 234 if len(value) == 0 { 235 errors = append(errors, fmt.Errorf( 236 "%q cannot be an empty string: %q", k, value)) 237 } 238 if regexp.MustCompile(`-$`).MatchString(value) { 239 errors = append(errors, fmt.Errorf( 240 "%q cannot end with a hyphen: %q", k, value)) 241 } 242 243 return 244 }