Skip to content

Commit aace8e3

Browse files
committed
Add cloudstack_role data source and resource implementation
- Implement data source for cloudstack_role with read functionality. - Create resource for managing cloudstack_role with CRUD operations. - Update documentation for cloudstack_role data source and resource.
1 parent c5b50a2 commit aace8e3

File tree

8 files changed

+522
-0
lines changed

8 files changed

+522
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"fmt"
24+
"log"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
)
29+
30+
func dataSourceCloudstackRole() *schema.Resource {
31+
return &schema.Resource{
32+
Read: dataSourceCloudstackRoleRead,
33+
Schema: map[string]*schema.Schema{
34+
"filter": dataSourceFiltersSchema(),
35+
36+
"id": {
37+
Type: schema.TypeString,
38+
Optional: true,
39+
Computed: true,
40+
},
41+
42+
"name": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
Computed: true,
46+
},
47+
48+
"type": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
53+
"description": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
58+
"is_public": {
59+
Type: schema.TypeBool,
60+
Computed: true,
61+
},
62+
},
63+
}
64+
}
65+
66+
func dataSourceCloudstackRoleRead(d *schema.ResourceData, meta interface{}) error {
67+
cs := meta.(*cloudstack.CloudStackClient)
68+
69+
var err error
70+
var role *cloudstack.Role
71+
72+
if id, ok := d.GetOk("id"); ok {
73+
log.Printf("[DEBUG] Getting Role by ID: %s", id.(string))
74+
role, _, err = cs.Role.GetRoleByID(id.(string))
75+
} else if name, ok := d.GetOk("name"); ok {
76+
log.Printf("[DEBUG] Getting Role by name: %s", name.(string))
77+
role, _, err = cs.Role.GetRoleByName(name.(string))
78+
} else {
79+
return fmt.Errorf("Either 'id' or 'name' must be specified")
80+
}
81+
82+
if err != nil {
83+
return err
84+
}
85+
86+
d.SetId(role.Id)
87+
d.Set("name", role.Name)
88+
d.Set("type", role.Type)
89+
d.Set("description", role.Description)
90+
d.Set("is_public", role.Ispublic)
91+
92+
return nil
93+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"testing"
24+
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
26+
)
27+
28+
func TestAccDataSourceCloudStackRole_basic(t *testing.T) {
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: testAccProviders,
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccDataSourceCloudStackRole_basic,
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr(
37+
"data.cloudstack_role.role", "name", "terraform-role"),
38+
resource.TestCheckResourceAttr(
39+
"data.cloudstack_role.role", "description", "terraform test role"),
40+
resource.TestCheckResourceAttr(
41+
"data.cloudstack_role.role", "is_public", "true"),
42+
),
43+
},
44+
},
45+
})
46+
}
47+
48+
const testAccDataSourceCloudStackRole_basic = `
49+
resource "cloudstack_role" "foo" {
50+
name = "terraform-role"
51+
description = "terraform test role"
52+
is_public = true
53+
}
54+
55+
data "cloudstack_role" "role" {
56+
name = "${cloudstack_role.foo.name}"
57+
}
58+
`

cloudstack/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func Provider() *schema.Provider {
9090
"cloudstack_user": dataSourceCloudstackUser(),
9191
"cloudstack_vpn_connection": dataSourceCloudstackVPNConnection(),
9292
"cloudstack_pod": dataSourceCloudstackPod(),
93+
"cloudstack_role": dataSourceCloudstackRole(),
9394
},
9495

9596
ResourcesMap: map[string]*schema.Resource{
@@ -131,6 +132,7 @@ func Provider() *schema.Provider {
131132
"cloudstack_account": resourceCloudStackAccount(),
132133
"cloudstack_user": resourceCloudStackUser(),
133134
"cloudstack_domain": resourceCloudStackDomain(),
135+
"cloudstack_role": resourceCloudStackRole(),
134136
},
135137

136138
ConfigureFunc: providerConfigure,
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"fmt"
24+
"log"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
)
29+
30+
func resourceCloudStackRole() *schema.Resource {
31+
return &schema.Resource{
32+
Create: resourceCloudStackRoleCreate,
33+
Read: resourceCloudStackRoleRead,
34+
Update: resourceCloudStackRoleUpdate,
35+
Delete: resourceCloudStackRoleDelete,
36+
Schema: map[string]*schema.Schema{
37+
"name": {
38+
Type: schema.TypeString,
39+
Required: true,
40+
},
41+
"type": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Computed: true,
45+
},
46+
"description": {
47+
Type: schema.TypeString,
48+
Optional: true,
49+
Computed: true,
50+
},
51+
"is_public": {
52+
Type: schema.TypeBool,
53+
Optional: true,
54+
Default: false,
55+
},
56+
},
57+
}
58+
}
59+
60+
func resourceCloudStackRoleCreate(d *schema.ResourceData, meta interface{}) error {
61+
cs := meta.(*cloudstack.CloudStackClient)
62+
name := d.Get("name").(string)
63+
64+
// Create a new parameter struct
65+
p := cs.Role.NewCreateRoleParams(name)
66+
67+
if roleType, ok := d.GetOk("type"); ok {
68+
p.SetType(roleType.(string))
69+
}
70+
71+
if description, ok := d.GetOk("description"); ok {
72+
p.SetDescription(description.(string))
73+
}
74+
75+
if isPublic, ok := d.GetOk("is_public"); ok {
76+
p.SetIspublic(isPublic.(bool))
77+
}
78+
79+
log.Printf("[DEBUG] Creating Role %s", name)
80+
r, err := cs.Role.CreateRole(p)
81+
82+
if err != nil {
83+
return fmt.Errorf("Error creating Role: %s", err)
84+
}
85+
86+
log.Printf("[DEBUG] Role %s successfully created", name)
87+
d.SetId(r.Id)
88+
89+
return resourceCloudStackRoleRead(d, meta)
90+
}
91+
92+
func resourceCloudStackRoleRead(d *schema.ResourceData, meta interface{}) error {
93+
cs := meta.(*cloudstack.CloudStackClient)
94+
95+
// Get the Role details
96+
r, count, err := cs.Role.GetRoleByID(d.Id())
97+
if err != nil {
98+
if count == 0 {
99+
log.Printf("[DEBUG] Role %s does not exist", d.Id())
100+
d.SetId("")
101+
return nil
102+
}
103+
return fmt.Errorf("Error getting Role: %s", err)
104+
}
105+
106+
d.Set("name", r.Name)
107+
d.Set("type", r.Type)
108+
d.Set("description", r.Description)
109+
d.Set("is_public", r.Ispublic)
110+
111+
return nil
112+
}
113+
114+
func resourceCloudStackRoleUpdate(d *schema.ResourceData, meta interface{}) error {
115+
cs := meta.(*cloudstack.CloudStackClient)
116+
117+
// Create a new parameter struct
118+
p := cs.Role.NewUpdateRoleParams(d.Id())
119+
120+
if d.HasChange("name") {
121+
p.SetName(d.Get("name").(string))
122+
}
123+
124+
if d.HasChange("type") {
125+
p.SetType(d.Get("type").(string))
126+
}
127+
128+
if d.HasChange("description") {
129+
p.SetDescription(d.Get("description").(string))
130+
}
131+
132+
if d.HasChange("is_public") {
133+
p.SetIspublic(d.Get("is_public").(bool))
134+
}
135+
136+
log.Printf("[DEBUG] Updating Role %s", d.Get("name").(string))
137+
_, err := cs.Role.UpdateRole(p)
138+
139+
if err != nil {
140+
return fmt.Errorf("Error updating Role: %s", err)
141+
}
142+
143+
return resourceCloudStackRoleRead(d, meta)
144+
}
145+
146+
func resourceCloudStackRoleDelete(d *schema.ResourceData, meta interface{}) error {
147+
cs := meta.(*cloudstack.CloudStackClient)
148+
149+
// Create a new parameter struct
150+
p := cs.Role.NewDeleteRoleParams(d.Id())
151+
152+
log.Printf("[DEBUG] Deleting Role %s", d.Get("name").(string))
153+
_, err := cs.Role.DeleteRole(p)
154+
155+
if err != nil {
156+
return fmt.Errorf("Error deleting Role: %s", err)
157+
}
158+
159+
return nil
160+
}

0 commit comments

Comments
 (0)