github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/data_source_network_interface.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func dataSourceNetworkInterface() *schema.Resource { 12 return &schema.Resource{ 13 Read: dataSourceNetworkInterfaceRead, 14 15 Schema: map[string]*schema.Schema{ 16 "instance_id": { 17 Type: schema.TypeString, 18 Required: true, 19 }, 20 21 "instance_name": { 22 Type: schema.TypeString, 23 Required: true, 24 }, 25 26 "interface": { 27 Type: schema.TypeString, 28 Required: true, 29 }, 30 31 // Computed Values returned from the data source lookup 32 "dns": { 33 Type: schema.TypeList, 34 Computed: true, 35 Elem: &schema.Schema{Type: schema.TypeString}, 36 }, 37 38 "ip_address": { 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 43 "ip_network": { 44 Type: schema.TypeString, 45 Computed: true, 46 }, 47 48 "mac_address": { 49 Type: schema.TypeString, 50 Computed: true, 51 }, 52 53 "model": { 54 Type: schema.TypeString, 55 Computed: true, 56 }, 57 58 "name_servers": { 59 Type: schema.TypeList, 60 Computed: true, 61 Elem: &schema.Schema{Type: schema.TypeString}, 62 }, 63 64 "nat": { 65 Type: schema.TypeList, 66 Computed: true, 67 Elem: &schema.Schema{Type: schema.TypeString}, 68 }, 69 70 "search_domains": { 71 Type: schema.TypeList, 72 Computed: true, 73 Elem: &schema.Schema{Type: schema.TypeString}, 74 }, 75 76 "sec_lists": { 77 Type: schema.TypeList, 78 Computed: true, 79 Elem: &schema.Schema{Type: schema.TypeString}, 80 }, 81 82 "shared_network": { 83 Type: schema.TypeBool, 84 Computed: true, 85 }, 86 87 "vnic": { 88 Type: schema.TypeString, 89 Computed: true, 90 }, 91 92 "vnic_sets": { 93 Type: schema.TypeList, 94 Computed: true, 95 Elem: &schema.Schema{Type: schema.TypeString}, 96 }, 97 }, 98 } 99 } 100 101 func dataSourceNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) error { 102 client := meta.(*compute.Client).Instances() 103 104 // Get required attributes 105 instance_name := d.Get("instance_name").(string) 106 instance_id := d.Get("instance_id").(string) 107 targetInterface := d.Get("interface").(string) 108 109 // Get instance 110 input := &compute.GetInstanceInput{ 111 Name: instance_name, 112 ID: instance_id, 113 } 114 115 instance, err := client.GetInstance(input) 116 if err != nil { 117 if compute.WasNotFoundError(err) { 118 d.SetId("") 119 return nil 120 } 121 return fmt.Errorf("Error reading instance %q: %v", instance_name, err) 122 } 123 124 result := compute.NetworkingInfo{} 125 126 // If the target instance has no network interfaces, return 127 if instance.Networking == nil { 128 d.SetId("") 129 return nil 130 } 131 132 // Set the computed fields 133 result = instance.Networking[targetInterface] 134 135 // Check if the target interface exists or not 136 if &result == nil { 137 log.Printf("[WARN] %q networking interface not found on instance %q", targetInterface, instance_name) 138 } 139 140 d.SetId(fmt.Sprintf("%s-%s", instance_name, targetInterface)) 141 142 // vNIC is a required field for an IP Network interface, and can only be set if the network 143 // interface is inside an IP Network. Use this key to determine shared_network status 144 if result.Vnic != "" { 145 d.Set("shared_network", false) 146 } else { 147 d.Set("shared_network", true) 148 } 149 150 d.Set("ip_address", result.IPAddress) 151 d.Set("ip_network", result.IPNetwork) 152 d.Set("mac_address", result.MACAddress) 153 d.Set("model", result.Model) 154 d.Set("vnic", result.Vnic) 155 156 if err := setStringList(d, "dns", result.DNS); err != nil { 157 return err 158 } 159 if err := setStringList(d, "name_servers", result.NameServers); err != nil { 160 return err 161 } 162 if err := setStringList(d, "nat", result.Nat); err != nil { 163 return err 164 } 165 if err := setStringList(d, "search_domains", result.SearchDomains); err != nil { 166 return err 167 } 168 if err := setStringList(d, "sec_lists", result.SecLists); err != nil { 169 return err 170 } 171 if err := setStringList(d, "vnic_sets", result.VnicSets); err != nil { 172 return err 173 } 174 175 return nil 176 }