-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_subnet_reserved_ip.go
132 lines (118 loc) · 3.65 KB
/
data_source_ibm_is_subnet_reserved_ip.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// Define all the constants that matches with the given terrafrom attribute
const (
// Request Param Constants
isSubNetID = "subnet"
isReservedIPID = "reserved_ip"
// Response Param Constants
isReservedIPAddress = "address"
isReservedIPAutoDelete = "auto_delete"
isReservedIPCreatedAt = "created_at"
isReservedIPhref = "href"
isReservedIPName = "name"
isReservedIPOwner = "owner"
isReservedIPType = "resource_type"
)
func DataSourceIBMISReservedIP() *schema.Resource {
return &schema.Resource{
Read: dataSdataSourceIBMISReservedIPRead,
Schema: map[string]*schema.Schema{
/*
Request Parameters
==================
These are mandatory req parameters
*/
isSubNetID: {
Type: schema.TypeString,
Required: true,
Description: "The subnet identifier.",
},
isReservedIPID: {
Type: schema.TypeString,
Required: true,
Description: "The reserved IP identifier.",
},
/*
Response Parameters
===================
All of these are computed and an user doesn't need to provide
these from outside.
*/
isReservedIPAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP address",
},
isReservedIPAutoDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, this reserved IP will be automatically deleted",
},
isReservedIPCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the reserved IP was created.",
},
isReservedIPhref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP.",
},
isReservedIPName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined or system-provided name for this reserved IP.",
},
isReservedIPOwner: {
Type: schema.TypeString,
Computed: true,
Description: "The owner of a reserved IP, defining whether it is managed by the user or the provider.",
},
isReservedIPType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
isReservedIPTarget: {
Type: schema.TypeString,
Computed: true,
Description: "Reserved IP target id.",
},
},
}
}
// dataSdataSourceIBMISReservedIPRead is used when the reserved IPs are read from the vpc
func dataSdataSourceIBMISReservedIPRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
subnetID := d.Get(isSubNetID).(string)
reservedIPID := d.Get(isReservedIPID).(string)
options := sess.NewGetSubnetReservedIPOptions(subnetID, reservedIPID)
reserveIP, response, err := sess.GetSubnetReservedIP(options)
if err != nil || response == nil || reserveIP == nil {
return fmt.Errorf("[ERROR] Error fetching the reserved IP %s\n%s", err, response)
}
d.SetId(*reserveIP.ID)
d.Set(isReservedIPAutoDelete, *reserveIP.AutoDelete)
d.Set(isReservedIPCreatedAt, (*reserveIP.CreatedAt).String())
d.Set(isReservedIPhref, *reserveIP.Href)
d.Set(isReservedIPName, *reserveIP.Name)
d.Set(isReservedIPOwner, *reserveIP.Owner)
d.Set(isReservedIPType, *reserveIP.ResourceType)
if reserveIP.Target != nil {
target, ok := reserveIP.Target.(*vpcv1.ReservedIPTarget)
if ok {
d.Set(isReservedIPTarget, target.ID)
}
}
return nil // By default there should be no error
}