-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_bare_metal_server_network_interface_reserved_ip.go
138 lines (124 loc) · 4.57 KB
/
data_source_ibm_is_bare_metal_server_network_interface_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
133
134
135
136
137
138
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// Define all the constants that matches with the given terrafrom attribute
const (
// Response Param Constants
isBareMetalServerNICReservedIPCreatedAt = "created_at"
isBareMetalServerNICReservedIPhref = "href"
isBareMetalServerNICReservedIPLifecycleState = "lifecycle_state"
isBareMetalServerNICReservedIPOwner = "owner"
isBareMetalServerNICReservedIPType = "resource_type"
isBareMetalServerNICReservedIPTarget = "target"
)
func DataSourceIBMISBareMetalServerNICReservedIP() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISBareMetalServerNICReservedIPRead,
Schema: map[string]*schema.Schema{
/*
Request Parameters
==================
These are mandatory req parameters
*/
isBareMetalServerID: {
Type: schema.TypeString,
Required: true,
Description: "The Bare Metal Server identifier.",
},
isBareMetalServerNicID: {
Type: schema.TypeString,
Required: true,
Description: "The Bare Metal Server network interface identifier.",
},
isBareMetalServerNicIpID: {
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.
*/
isBareMetalServerNicIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP address",
},
isBareMetalServerNicIpAutoDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, this reserved IP will be automatically deleted",
},
isBareMetalServerNICReservedIPCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the reserved IP was created.",
},
isBareMetalServerNICReservedIPhref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP.",
},
isBareMetalServerNicIpName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined or system-provided name for this reserved IP.",
},
isBareMetalServerNICReservedIPOwner: {
Type: schema.TypeString,
Computed: true,
Description: "The owner of a reserved IP, defining whether it is managed by the user or the provider.",
},
isBareMetalServerNICReservedIPType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
isBareMetalServerNICReservedIPTarget: {
Type: schema.TypeString,
Computed: true,
Description: "Reserved IP target id.",
},
},
}
}
// dataSourceIBMISBareMetalServerNICReservedIPRead is used when the reserved IPs are read from the vpc
func dataSourceIBMISBareMetalServerNICReservedIPRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
bareMetalServer := d.Get(isBareMetalServerID).(string)
bareMetalServerNICId := d.Get(isBareMetalServerNicID).(string)
reservedIPID := d.Get(isBareMetalServerNicIpID).(string)
options := sess.NewGetBareMetalServerNetworkInterfaceIPOptions(bareMetalServer, bareMetalServerNICId, reservedIPID)
reserveIP, response, err := sess.GetBareMetalServerNetworkInterfaceIPWithContext(context, options)
if err != nil || response == nil || reserveIP == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching the reserved IP %s\n%s", err, response))
}
d.SetId(*reserveIP.ID)
d.Set(isBareMetalServerNicIpAutoDelete, *reserveIP.AutoDelete)
d.Set(isBareMetalServerNICReservedIPCreatedAt, (*reserveIP.CreatedAt).String())
d.Set(isBareMetalServerNICReservedIPhref, *reserveIP.Href)
d.Set(isBareMetalServerNicIpName, *reserveIP.Name)
d.Set(isBareMetalServerNICReservedIPOwner, *reserveIP.Owner)
d.Set(isBareMetalServerNICReservedIPType, *reserveIP.ResourceType)
d.Set(isBareMetalServerNicIpAddress, *reserveIP.Address)
if reserveIP.Target != nil {
target, ok := reserveIP.Target.(*vpcv1.ReservedIPTarget)
if ok {
d.Set(isBareMetalServerNICReservedIPTarget, target.ID)
}
}
return nil // By default there should be no error
}