Copilot commented on code in PR #256:
URL: 
https://github.com/apache/cloudstack-terraform-provider/pull/256#discussion_r2450727621


##########
cloudstack/data_source_cloudstack_user_data.go:
##########
@@ -0,0 +1,131 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "encoding/base64"
+       "fmt"
+       "log"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+)
+
+func dataSourceCloudstackUserData() *schema.Resource {
+       return &schema.Resource{
+               Read: dataSourceCloudstackUserDataRead,
+               Schema: map[string]*schema.Schema{
+                       "name": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                       },
+                       "account": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "account_id": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "domain": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "domain_id": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "project": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "project_id": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "userdata_id": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "userdata": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+                       "params": {
+                               Type:     schema.TypeString,
+                               Computed: true,
+                       },
+               },
+       }
+}
+
+func dataSourceCloudstackUserDataRead(d *schema.ResourceData, meta 
interface{}) error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       name := d.Get("name").(string)
+       p := cs.User.NewListUserDataParams()
+       p.SetName(name)
+
+       if v, ok := d.GetOk("account"); ok {
+               p.SetAccount(v.(string))
+       }
+       if v, ok := d.GetOk("domain_id"); ok {
+               p.SetDomainid(v.(string))
+       }
+
+       log.Printf("[DEBUG] Listing user data with name: %s", name)
+       userdataList, err := cs.User.ListUserData(p)
+       if err != nil {
+               return fmt.Errorf("Error listing user data with name %s: %s", 
name, err)
+       }
+
+       if len(userdataList.UserData) == 0 {
+               return fmt.Errorf("No user data found with name: %s", name)
+       }
+       if len(userdataList.UserData) > 1 {
+               return fmt.Errorf("Multiple user data entries found with name: 
%s", name)
+       }
+
+       userdata := userdataList.UserData[0]
+
+       d.SetId(userdata.Id)
+       d.Set("name", userdata.Name)
+       d.Set("account", userdata.Account)
+       d.Set("account_id", userdata.Accountid)
+       d.Set("domain", userdata.Domain)
+       d.Set("domain_id", userdata.Domainid)
+       d.Set("userdata_id", userdata.Id)
+       d.Set("params", userdata.Params)
+
+       if userdata.Project != "" {
+               d.Set("project", userdata.Project)
+               d.Set("project_id", userdata.Projectid)
+       }
+
+       if userdata.Userdata != "" {
+               decoded, err := 
base64.StdEncoding.DecodeString(userdata.Userdata)
+               if err != nil {
+                       d.Set("userdata", userdata.Userdata) // Fallback: use 
raw data
+               } else {
+                       d.Set("userdata", decoded)

Review Comment:
   The decoded byte slice should be converted to a string before setting. Use 
`string(decoded)` to properly convert the decoded bytes to a string value.
   ```suggestion
                        d.Set("userdata", string(decoded))
   ```



##########
cloudstack/data_source_cloudstack_user_data.go:
##########
@@ -0,0 +1,131 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "encoding/base64"
+       "fmt"
+       "log"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+)
+
+func dataSourceCloudstackUserData() *schema.Resource {
+       return &schema.Resource{
+               Read: dataSourceCloudstackUserDataRead,
+               Schema: map[string]*schema.Schema{
+                       "name": {
+                               Type:     schema.TypeString,
+                               Required: true,

Review Comment:
   The data source schema defines 'name' as Required but the documentation 
shows usage with a 'filter' block. The schema should use a 'filter' field 
pattern consistent with other CloudStack data sources, or the documentation 
should be updated to match the actual implementation.
   ```suggestion
                        "filter": {
                                Type:     schema.TypeList,
                                Optional: true,
                                Elem: &schema.Resource{
                                        Schema: map[string]*schema.Schema{
                                                "name": {
                                                        Type:     
schema.TypeString,
                                                        Required: true,
                                                },
                                                "value": {
                                                        Type:     
schema.TypeString,
                                                        Required: true,
                                                },
                                        },
                                },
   ```



##########
cloudstack/resource_cloudstack_user_data.go:
##########
@@ -0,0 +1,166 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "fmt"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+)
+
+func resourceCloudStackUserData() *schema.Resource {
+       return &schema.Resource{
+               Create: resourceCloudStackUserDataCreate,
+               Read:   resourceCloudStackUserDataRead,
+               Delete: resourceCloudStackUserDataDelete,
+               Importer: &schema.ResourceImporter{
+                       State: importStatePassthrough,
+               },
+
+               Schema: map[string]*schema.Schema{
+                       "name": {
+                               Type:        schema.TypeString,
+                               Required:    true,
+                               ForceNew:    true,
+                               Description: "Name of the user data",
+                       },
+
+                       "userdata": {
+                               Type:        schema.TypeString,
+                               Required:    true,
+                               ForceNew:    true,
+                               Description: "The user data content to be 
registered",
+                       },
+
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "An optional account for the user 
data. Must be used with domain_id.",
+                       },
+
+                       "domain_id": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "An optional domain ID for the 
user data. If the account parameter is used, domain_id must also be used.",
+                       },
+
+                       "params": {
+                               Type:        schema.TypeSet,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Optional comma separated list of 
variables declared in user data content.",
+                               Elem: &schema.Schema{
+                                       Type: schema.TypeString,
+                               },
+                       },
+
+                       "project_id": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "An optional project for the user 
data.",
+                       },
+               },
+       }
+}
+
+func resourceCloudStackUserDataCreate(d *schema.ResourceData, meta 
interface{}) error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       p := cs.User.NewRegisterUserDataParams(d.Get("name").(string), 
d.Get("userdata").(string))
+       if v, ok := d.GetOk("account"); ok {
+               p.SetAccount(v.(string))
+       }
+       if v, ok := d.GetOk("domain_id"); ok {
+               p.SetDomainid(v.(string))
+       }
+       if v, ok := d.GetOk("project_id"); ok {
+               p.SetProjectid(v.(string))
+       }
+       if v, ok := d.GetOk("params"); ok {
+               paramsList := v.(*schema.Set).List()
+               var params []string
+               for _, param := range paramsList {
+                       params = append(params, param.(string))
+               }
+               p.SetParams(strings.Join(params, ","))
+       }
+
+       userdata, err := cs.User.RegisterUserData(p)
+       if err != nil {
+               return fmt.Errorf("Error registering user data: %s", err)
+       }
+
+       d.SetId(userdata.Id)
+
+       return resourceCloudStackUserDataRead(d, meta)
+}
+
+func resourceCloudStackUserDataRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       id := d.Id()
+
+       p := cs.User.NewListUserDataParams()
+       p.SetId(id)
+
+       userdata, err := cs.User.ListUserData(p)
+       if err != nil {
+               return fmt.Errorf("Error retrieving user data with ID %s: %s", 
id, err)
+       }
+
+       d.Set("name", userdata.UserData[0].Name)
+       d.Set("userdata", userdata.UserData[0])

Review Comment:
   The code is setting the userdata field to the entire UserData object instead 
of extracting the Userdata property. This should be 
`userdata.UserData[0].Userdata` to set the actual userdata content string.
   ```suggestion
        d.Set("userdata", userdata.UserData[0].Userdata)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to