vatesfr / terraform-provider-xenorchestra

Xen Orchestra provider for Terraform
MIT License
150 stars 32 forks source link

Unify cloud config client code with the other objects' design #118

Open ddelnano opened 3 years ago

ddelnano commented 3 years ago

While adding support for the cloud config data source, I realized that the GetCloudConfig method returns nil, nil in the case that the cloud config is not found. This api should be updated to behave more similarly to the other XO api methods (which return a client.NotFound error in these cases). Unfortunately making this change causes all the cloud config resource acceptance tests to fail so more investigation needs to be done on how to migrate the code to use the other pattern.

Below is a git stash of the refactor that causes the tests to fail

diff --git a/client/cloud_config.go b/client/cloud_config.go
index bfd140c..a6c8f21 100644
--- a/client/cloud_config.go
+++ b/client/cloud_config.go
@@ -44,7 +44,7 @@ func (c *Client) GetCloudConfig(id string) (*CloudConfig, error) {
                }
        }

-       return nil, nil
+       return nil, NotFound{Query: cloudConfig}
 }

 func (c *Client) GetCloudConfigByName(name string) ([]CloudConfig, error) {
diff --git a/xoa/resource_xenorchestra_cloud_config.go b/xoa/resource_xenorchestra_cloud_config.go
index bcc9319..02fe873 100644
--- a/xoa/resource_xenorchestra_cloud_config.go
+++ b/xoa/resource_xenorchestra_cloud_config.go
@@ -44,15 +44,16 @@ func resourceCloudConfigRead(d *schema.ResourceData, m interface{}) error {
        c := m.(*client.Client)

        cloud_config, err := c.GetCloudConfig(d.Id())
-       if err != nil {
-               return err
-       }

-       if cloud_config == nil {
+       if _, ok := err.(client.NotFound); ok {
                d.SetId("")
                return nil
        }

+       if err != nil {
+               return err
+       }
+
        d.Set("name", cloud_config.Name)
        d.Set("template", cloud_config.Template)
        return nil
@@ -75,6 +76,10 @@ func CloudConfigImport(d *schema.ResourceData, m interface{}) ([]*schema.Resourc
        c := m.(*client.Client)

        cloud_config, err := c.GetCloudConfig(d.Id())
+       if _, ok := err.(client.NotFound); ok {
+               d.SetId("")
+               return []*schema.ResourceData{d}, nil
+       }

        if err != nil {
                return nil, err