-
Notifications
You must be signed in to change notification settings - Fork 658
/
resource_ibm_dns_reverse_record.go
149 lines (137 loc) · 4.95 KB
/
resource_ibm_dns_reverse_record.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package classicinfrastructure
import (
"fmt"
"log"
"strconv"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/sl"
)
func ResourceIBMDNSReverseRecord() *schema.Resource {
return &schema.Resource{
Exists: resourceIBMDNSREVERSERecordExists,
Create: resourceIBMDNSREVERSERecordCreate,
Read: resourceIBMDNSREVERSERecordRead,
Update: resourceIBMDNSREVERSERecordUpdate,
Delete: resourceIBMDNSREVERSERecordDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"ipaddress": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "IP Address",
},
"hostname": {
Type: schema.TypeString,
Required: true,
Description: "Host name",
},
"ttl": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: func() (interface{}, error) {
return 604800, nil
},
Description: "TTL value",
},
},
}
}
// Creates DNS Domain Reverse Record
// https://sldn.softlayer.com/reference/services/SoftLayer_Dns_Domain/CreatePtrRecord
func resourceIBMDNSREVERSERecordCreate(d *schema.ResourceData, meta interface{}) error {
sess := meta.(conns.ClientSession).SoftLayerSession()
service := services.GetDnsDomainService(sess.SetRetries(0))
Data := sl.String(d.Get("hostname").(string))
Ttl := sl.Int(d.Get("ttl").(int))
Ipaddress := sl.String(d.Get("ipaddress").(string))
var id int
record, err := service.CreatePtrRecord(Ipaddress, Data, Ttl)
if record.Id != nil {
id = *record.Id
}
if err != nil {
return fmt.Errorf("[ERROR] Error creating DNS Reverse %s", err)
}
d.SetId(fmt.Sprintf("%d", id))
log.Printf("[INFO] Dns Reverse %s ", d.Id())
return resourceIBMDNSREVERSERecordRead(d, meta)
}
// Reads DNS Domain Reverse Record from SL system
// https://sldn.softlayer.com/reference/services/SoftLayer_Dns_Domain_ResourceRecord/getObject
func resourceIBMDNSREVERSERecordRead(d *schema.ResourceData, meta interface{}) error {
sess := meta.(conns.ClientSession).SoftLayerSession()
service := services.GetDnsDomainResourceRecordService(sess)
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("[ERROR] Not a valid ID, must be an integer: %s", err)
}
_, nexterr := service.Id(id).GetObject()
if nexterr != nil {
return fmt.Errorf("[ERROR] Error retrieving DNS Reverse Record: %s", err)
}
return nil
}
// Updates DNS Domain Reverse Record in SL system
// https://sldn.softlayer.com/reference/services/SoftLayer_Dns_Domain_ResourceRecord/editObject
func resourceIBMDNSREVERSERecordUpdate(d *schema.ResourceData, meta interface{}) error {
sess := meta.(conns.ClientSession).SoftLayerSession()
service := services.GetDnsDomainResourceRecordService(sess)
serviceNoRetry := services.GetDnsDomainResourceRecordService(sess.SetRetries(0))
recordId, _ := strconv.Atoi(d.Id())
record, err := service.Id(recordId).GetObject()
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving DNS Reverse Record: %s", err)
}
if data, ok := d.GetOk("hostname"); ok && d.HasChange("hostname") {
record.Data = sl.String(data.(string))
}
if ttl, ok := d.GetOk("ttl"); ok && d.HasChange("ttl") {
record.Ttl = sl.Int(ttl.(int))
}
record.IsGatewayAddress = nil
_, err = serviceNoRetry.Id(recordId).EditObject(&record)
if err != nil {
return fmt.Errorf("[ERROR] Error editing DNS Reverse Record %d: %s", recordId, err)
}
return nil
}
// Deletes DNS Domain Reverse Record in SL system
// https://sldn.softlayer.com/reference/services/SoftLayer_Dns_Domain_ResourceRecord/deleteObject
func resourceIBMDNSREVERSERecordDelete(d *schema.ResourceData, meta interface{}) error {
sess := meta.(conns.ClientSession).SoftLayerSession()
service := services.GetDnsDomainResourceRecordService(sess)
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("[ERROR] Not a valid ID, must be an integer: %s", err)
}
_, err = service.Id(id).DeleteObject()
if err != nil {
return fmt.Errorf("[ERROR] Error deleting DNS Reverse Record: %s", err)
}
return nil
}
// Exists function is called by refresh
// if the entity is absent - it is deleted from the .tfstate file
func resourceIBMDNSREVERSERecordExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess := meta.(conns.ClientSession).SoftLayerSession()
service := services.GetDnsDomainResourceRecordService(sess)
id, err := strconv.Atoi(d.Id())
if err != nil {
return false, fmt.Errorf("[ERROR] Not a valid ID, must be an integer: %s", err)
}
record, err := service.Id(id).GetObject()
if err != nil {
if apiErr, ok := err.(sl.Error); ok {
if apiErr.StatusCode == 404 {
return false, nil
}
}
return false, fmt.Errorf("[ERROR] Error retrieving domain reverse record info: %s", err)
}
return record.Id != nil && *record.Id == id, nil
}