Skip to content

Commit e465c8a

Browse files
committed
add userdata
1 parent e53f30c commit e465c8a

10 files changed

+1192
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
"encoding/base64"
24+
"fmt"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
)
29+
30+
func dataSourceCloudstackUserData() *schema.Resource {
31+
return &schema.Resource{
32+
Read: dataSourceCloudstackUserDataRead,
33+
Schema: map[string]*schema.Schema{
34+
"name": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
},
38+
39+
"account": {
40+
Type: schema.TypeString,
41+
Optional: true,
42+
Computed: true,
43+
},
44+
45+
"project": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Computed: true,
49+
},
50+
51+
"params": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
56+
"domain": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
},
60+
61+
"domain_id": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
},
65+
66+
"account_id": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
},
70+
71+
"user_data": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
},
75+
},
76+
}
77+
}
78+
79+
func dataSourceCloudstackUserDataRead(d *schema.ResourceData, meta interface{}) error {
80+
cs := meta.(*cloudstack.CloudStackClient)
81+
82+
p := cs.User.NewListUserDataParams()
83+
p.SetName(d.Get("name").(string))
84+
85+
if account, ok := d.GetOk("account"); ok {
86+
p.SetAccount(account.(string))
87+
}
88+
89+
if project, ok := d.GetOk("project"); ok {
90+
if project.(string) != "" {
91+
projectid, retrieveErr := retrieveID(cs, "project", project.(string))
92+
if retrieveErr != nil {
93+
return retrieveErr.Error()
94+
}
95+
p.SetProjectid(projectid)
96+
}
97+
}
98+
99+
resp, err := cs.User.ListUserData(p)
100+
if err != nil {
101+
return fmt.Errorf("Error listing UserData: %s", err)
102+
}
103+
104+
if resp.Count == 0 || len(resp.UserData) == 0 {
105+
return fmt.Errorf("UserData %s not found", d.Get("name").(string))
106+
}
107+
108+
if resp.Count > 1 && len(resp.UserData) > 1 {
109+
return fmt.Errorf("Multiple UserData entries found for name %s", d.Get("name").(string))
110+
}
111+
112+
userData := resp.UserData[0]
113+
114+
d.SetId(userData.Id)
115+
d.Set("name", userData.Name)
116+
d.Set("account", userData.Account)
117+
d.Set("account_id", userData.Accountid)
118+
d.Set("domain", userData.Domain)
119+
d.Set("domain_id", userData.Domainid)
120+
d.Set("params", userData.Params)
121+
122+
if userData.Project != "" {
123+
d.Set("project", userData.Project)
124+
}
125+
126+
if userData.Userdata != "" {
127+
decoded, err := base64.StdEncoding.DecodeString(userData.Userdata)
128+
if err != nil {
129+
d.Set("user_data", userData.Userdata)
130+
} else {
131+
d.Set("user_data", string(decoded))
132+
}
133+
}
134+
135+
return nil
136+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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-testing/helper/resource"
26+
)
27+
28+
func TestAccDataSourceCloudStackUserData_basic(t *testing.T) {
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: testAccProviders,
32+
CheckDestroy: testAccCheckCloudStackUserDataDestroy,
33+
Steps: []resource.TestStep{
34+
{
35+
Config: testAccDataSourceCloudStackUserDataConfig,
36+
Check: resource.ComposeTestCheckFunc(
37+
resource.TestCheckResourceAttr("data.cloudstack_user_data.test", "name", "terraform-test-userdata"),
38+
resource.TestCheckResourceAttr("data.cloudstack_user_data.test", "user_data", "#!/bin/bash\\necho 'Hello World'\\n"),
39+
),
40+
},
41+
},
42+
})
43+
}
44+
45+
const testAccDataSourceCloudStackUserDataConfig = `
46+
resource "cloudstack_user_data" "test" {
47+
name = "terraform-test-userdata"
48+
user_data = <<-EOF
49+
#!/bin/bash
50+
echo 'Hello World'
51+
EOF
52+
}
53+
54+
data "cloudstack_user_data" "test" {
55+
name = cloudstack_user_data.test.name
56+
}
57+
`

cloudstack/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func Provider() *schema.Provider {
8383
"cloudstack_autoscale_vm_profile": dataSourceCloudstackAutoscaleVMProfile(),
8484
"cloudstack_condition": dataSourceCloudstackCondition(),
8585
"cloudstack_counter": dataSourceCloudstackCounter(),
86+
"cloudstack_user_data": dataSourceCloudstackUserData(),
8687
"cloudstack_template": dataSourceCloudstackTemplate(),
8788
"cloudstack_ssh_keypair": dataSourceCloudstackSSHKeyPair(),
8889
"cloudstack_instance": dataSourceCloudstackInstance(),
@@ -155,6 +156,8 @@ func Provider() *schema.Provider {
155156
"cloudstack_account": resourceCloudStackAccount(),
156157
"cloudstack_project": resourceCloudStackProject(),
157158
"cloudstack_user": resourceCloudStackUser(),
159+
"cloudstack_user_data": resourceCloudStackUserData(),
160+
"cloudstack_user_data_template_link": resourceCloudStackUserDataTemplateLink(),
158161
"cloudstack_domain": resourceCloudStackDomain(),
159162
"cloudstack_network_service_provider": resourceCloudStackNetworkServiceProvider(),
160163
"cloudstack_role": resourceCloudStackRole(),

0 commit comments

Comments
 (0)