github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/data_source_arm_public_ip.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func dataSourceArmPublicIP() *schema.Resource {
    11  	return &schema.Resource{
    12  		Read: dataSourceArmPublicIPRead,
    13  		Schema: map[string]*schema.Schema{
    14  			"name": {
    15  				Type:     schema.TypeString,
    16  				Required: true,
    17  			},
    18  
    19  			"resource_group_name": {
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  			},
    23  
    24  			"domain_name_label": {
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  
    29  			"idle_timeout_in_minutes": {
    30  				Type:     schema.TypeInt,
    31  				Computed: true,
    32  			},
    33  
    34  			"fqdn": {
    35  				Type:     schema.TypeString,
    36  				Computed: true,
    37  			},
    38  
    39  			"ip_address": {
    40  				Type:     schema.TypeString,
    41  				Computed: true,
    42  			},
    43  
    44  			"tags": tagsSchema(),
    45  		},
    46  	}
    47  }
    48  
    49  func dataSourceArmPublicIPRead(d *schema.ResourceData, meta interface{}) error {
    50  	publicIPClient := meta.(*ArmClient).publicIPClient
    51  
    52  	resGroup := d.Get("resource_group_name").(string)
    53  	name := d.Get("name").(string)
    54  
    55  	resp, err := publicIPClient.Get(resGroup, name, "")
    56  	if err != nil {
    57  		if resp.StatusCode == http.StatusNotFound {
    58  			d.SetId("")
    59  		}
    60  		return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err)
    61  	}
    62  
    63  	d.SetId(*resp.ID)
    64  
    65  	if resp.PublicIPAddressPropertiesFormat.DNSSettings != nil {
    66  
    67  		if resp.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != nil && *resp.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn != "" {
    68  			d.Set("fqdn", resp.PublicIPAddressPropertiesFormat.DNSSettings.Fqdn)
    69  		}
    70  
    71  		if resp.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != nil && *resp.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel != "" {
    72  			d.Set("domain_name_label", resp.PublicIPAddressPropertiesFormat.DNSSettings.DomainNameLabel)
    73  		}
    74  	}
    75  
    76  	if resp.PublicIPAddressPropertiesFormat.IPAddress != nil && *resp.PublicIPAddressPropertiesFormat.IPAddress != "" {
    77  		d.Set("ip_address", resp.PublicIPAddressPropertiesFormat.IPAddress)
    78  	}
    79  
    80  	if resp.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes != nil {
    81  		d.Set("idle_timeout_in_minutes", *resp.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes)
    82  	}
    83  
    84  	flattenAndSetTags(d, resp.Tags)
    85  	return nil
    86  }