/
datasource_addresslist.go
68 lines (55 loc) · 1.89 KB
/
datasource_addresslist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ddcloud
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
func dataSourceAddressList() *schema.Resource {
return &schema.Resource{
Read: dataSourceAddressListRead,
Schema: map[string]*schema.Schema{
resourceKeyAddressListNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The Id of the network domain in which the address list rule applies",
},
resourceKeyAddressListName: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "A name for the address list",
},
resourceKeyAddressListDescription: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A description for the firewall rule",
},
resourceKeyAddressListIPVersion: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IP version (IPv4 or IPv6) used by the address list",
// ConflictsWith: []string{resourceKeyAddressListIPVersion},
},
},
}
}
// Read a address list data source.
func dataSourceAddressListRead(data *schema.ResourceData, provider interface{}) error {
name := data.Get(resourceKeyAddressListName).(string)
domainId := data.Get(resourceKeyAddressListNetworkDomainID).(string)
log.Printf("Read address list '%s' from network domain '%s'.", name, domainId)
apiClient := provider.(*providerState).Client()
addressList, err := apiClient.GetIPAddressListByName(name, domainId)
if err != nil {
return err
}
if addressList != nil {
log.Printf("Found addresslist '%s'.", name)
data.SetId(addressList.ID)
data.Set(resourceKeyAddressListName, addressList.Name)
data.Set(resourceKeyAddressListDescription, addressList.Description)
data.Set(resourceKeyAddressListIPVersion, addressList.IPVersion)
} else {
return fmt.Errorf("failed to find Addresslist with name '%s'", name)
}
return nil
}