-
Notifications
You must be signed in to change notification settings - Fork 117
/
resource_bigip_cm_device.go
150 lines (122 loc) · 4.15 KB
/
resource_bigip_cm_device.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
139
140
141
142
143
144
145
146
147
148
149
150
/*
Original work from https://github.com/DealerDotCom/terraform-provider-bigip
Modifications Copyright 2019 F5 Networks Inc.
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file,You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package bigip
import (
"context"
"fmt"
"log"
bigip "github.com/f5devcentral/go-bigip"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceBigipCmDevice() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBigipCmDeviceCreate,
UpdateContext: resourceBigipCmDeviceUpdate,
ReadContext: resourceBigipCmDeviceRead,
DeleteContext: resourceBigipCmDeviceDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"configsync_ip": {
Type: schema.TypeString,
Required: true,
Description: "IP address used for config sync",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Address of the Device which needs to be Deviceensed",
},
"mirror_ip": {
Type: schema.TypeString,
Optional: true,
Description: "IP address used for state mirroring",
},
"mirror_secondary_ip": {
Type: schema.TypeString,
Optional: true,
Description: "Secondary IP address used for state mirroring",
},
},
}
}
func resourceBigipCmDeviceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
configsyncIp := d.Get("configsync_ip").(string)
name := d.Get("name").(string)
mirrorIp := d.Get("mirror_ip").(string)
mirrorSecondaryIp := d.Get("mirror_secondary_ip").(string)
log.Println("[INFO] Creating Device ")
err := client.CreateDevice(
name,
configsyncIp,
mirrorIp,
mirrorSecondaryIp,
)
if err != nil {
log.Printf("[ERROR] Unable to Create Device %s %v ", name, err)
return diag.FromErr(err)
}
d.SetId(name)
return resourceBigipCmDeviceRead(ctx, d, meta)
}
func resourceBigipCmDeviceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Updating Device " + name)
r := &bigip.Device{
Name: name,
ConfigsyncIp: d.Get("configsync_ip").(string),
MirrorIp: d.Get("mirror_ip").(string),
MirrorSecondaryIp: d.Get("mirror_secondary_ip").(string),
}
err := client.ModifyDevice(r)
if err != nil {
log.Printf("[ERROR] Unable to Modidy Device (%s) (%v) ", name, err)
return diag.FromErr(err)
}
return resourceBigipCmDeviceRead(ctx, d, meta)
}
func resourceBigipCmDeviceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Reading Device " + name)
members, err := client.Devices(name)
if err != nil {
log.Printf("[ERROR] Unable to retrieve Device (%s) (%v) ", name, err)
return diag.FromErr(err)
}
if members == nil {
log.Printf("[WARN] Device (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
_ = d.Set("name", members.Name)
if err := d.Set("mirror_ip", members.MirrorIp); err != nil {
return diag.FromErr(fmt.Errorf("[DEBUG] Error saving mirror_ip to state for Device (%s): %s", d.Id(), err))
}
if err := d.Set("configsync_ip", members.ConfigsyncIp); err != nil {
return diag.FromErr(fmt.Errorf("[DEBUG] Error saving configsync_ip to state for Device (%s): %s", d.Id(), err))
}
if err := d.Set("mirror_secondary_ip", members.MirrorSecondaryIp); err != nil {
return diag.FromErr(fmt.Errorf("[DEBUG] Error saving mirror_secondary_ip to state for Device (%s): %s", d.Id(), err))
}
return nil
}
func resourceBigipCmDeviceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
err := client.DeleteDevice(name)
if err != nil {
log.Printf("[ERROR] Unable to Delete Device (%s) (%v) ", name, err)
return diag.FromErr(err)
}
d.SetId("")
return nil
}