/
resource_ibm_app_domain_shared.go
128 lines (104 loc) · 3.18 KB
/
resource_ibm_app_domain_shared.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
package ibm
import (
"fmt"
v2 "github.com/IBM-Cloud/bluemix-go/api/mccp/mccpv2"
"github.com/IBM-Cloud/bluemix-go/bmxerror"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceIBMAppDomainShared() *schema.Resource {
return &schema.Resource{
Create: resourceIBMAppDomainSharedCreate,
Read: resourceIBMAppDomainSharedRead,
Update: resourceIBMAppDomainSharedUpdate,
Delete: resourceIBMAppDomainSharedDelete,
Exists: resourceIBMAppDomainSharedExists,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the domain",
ValidateFunc: validateDomainName,
},
"router_group_guid": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The guid of the router group.",
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func resourceIBMAppDomainSharedCreate(d *schema.ResourceData, meta interface{}) error {
cfClient, err := meta.(ClientSession).MccpAPI()
if err != nil {
return err
}
name := d.Get("name").(string)
routerGroupGUID := d.Get("router_group_guid").(string)
params := v2.SharedDomainRequest{
Name: name,
RouterGroupGUID: routerGroupGUID,
}
shdomain, err := cfClient.SharedDomains().Create(params)
if err != nil {
return fmt.Errorf("Error creating shared domain: %s", err)
}
d.SetId(shdomain.Metadata.GUID)
return resourceIBMAppDomainSharedRead(d, meta)
}
func resourceIBMAppDomainSharedUpdate(d *schema.ResourceData, meta interface{}) error {
//Only tags are updated and that too locally hence nothing to validate and update in terms of real API at this point
return nil
}
func resourceIBMAppDomainSharedRead(d *schema.ResourceData, meta interface{}) error {
cfClient, err := meta.(ClientSession).MccpAPI()
if err != nil {
return err
}
shdomainGUID := d.Id()
shdomain, err := cfClient.SharedDomains().Get(shdomainGUID)
if err != nil {
return fmt.Errorf("Error retrieving shared domain: %s", err)
}
d.Set("name", shdomain.Entity.Name)
d.Set("router_group_guid", shdomain.Entity.RouterGroupGUID)
return nil
}
func resourceIBMAppDomainSharedDelete(d *schema.ResourceData, meta interface{}) error {
cfClient, err := meta.(ClientSession).MccpAPI()
if err != nil {
return err
}
shdomainGUID := d.Id()
err = cfClient.SharedDomains().Delete(shdomainGUID, false)
if err != nil {
return fmt.Errorf("Error deleting shared domain: %s", err)
}
d.SetId("")
return nil
}
func resourceIBMAppDomainSharedExists(d *schema.ResourceData, meta interface{}) (bool, error) {
cfClient, err := meta.(ClientSession).MccpAPI()
if err != nil {
return false, err
}
shdomainGUID := d.Id()
shdomain, err := cfClient.SharedDomains().Get(shdomainGUID)
if err != nil {
if apiErr, ok := err.(bmxerror.RequestFailure); ok {
if apiErr.StatusCode() == 404 {
return false, nil
}
}
return false, fmt.Errorf("Error communicating with the API: %s", err)
}
return shdomain.Metadata.GUID == shdomainGUID, nil
}