-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_cf_service_plan_access.go
116 lines (102 loc) · 2.72 KB
/
resource_cf_service_plan_access.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
package cloudfoundry
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceServicePlanAccess() *schema.Resource {
return &schema.Resource{
CreateContext: resourceServicePlanAccessCreate,
ReadContext: resourceServicePlanAccessRead,
DeleteContext: resourceServicePlanAccessDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceServicePlanAccessImport,
},
Schema: map[string]*schema.Schema{
"plan": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"org": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"public"},
},
"public": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"org"},
},
},
}
}
func resourceServicePlanAccessCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
session := meta.(*managers.Session)
if session == nil {
return diag.Errorf("client is nil")
}
plan := d.Get("plan").(string)
public, hasPublic := d.GetOkExists("public")
org, hasOrg := d.GetOk("org")
var id string
if hasOrg {
spV, _, err := session.ClientV2.CreateServicePlanVisibility(plan, org.(string))
if err != nil {
return diag.FromErr(err)
}
id = spV.GUID
} else {
state := false
if hasPublic {
state = public.(bool)
}
_, err := session.ClientV2.UpdateServicePlan(plan, state)
if err != nil {
return diag.FromErr(err)
}
id = plan
}
d.SetId(id)
return nil
}
func resourceServicePlanAccessRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
session := meta.(*managers.Session)
_, hasOrg := d.GetOk("org")
if hasOrg {
spV, _, err := session.ClientV2.GetServicePlanVisibility(d.Id())
if err != nil {
if IsErrNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
d.Set("plan", spV.ServicePlanGUID)
d.Set("org", spV.OrganizationGUID)
} else {
plan, _, err := session.ClientV2.GetServicePlan(d.Id())
if err != nil {
if IsErrNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
d.Set("plan", d.Id())
d.Set("public", plan.Public)
}
return nil
}
func resourceServicePlanAccessDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
session := meta.(*managers.Session)
_, hasOrg := d.GetOk("org")
if !hasOrg {
return nil
}
_, err := session.ClientV2.DeleteServicePlanVisibility(d.Id())
return diag.FromErr(err)
}