Skip to content

Commit e953808

Browse files
committed
Fix catalog_item_entitlement lint and compile errors
Signed-off-by: Ferran Rodenas <[email protected]>
1 parent 81e382a commit e953808

8 files changed

+380
-323
lines changed
Lines changed: 100 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,117 @@
11
package vra
22

33
import (
4-
"fmt"
5-
"log"
4+
"fmt"
65

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/vmware/vra-sdk-go/pkg/client/catalog_entitlements"
9-
"github.com/vmware/vra-sdk-go/pkg/models"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/vmware/vra-sdk-go/pkg/client/catalog_entitlements"
108
)
119

1210
func dataSourceCatalogItemEntitlement() *schema.Resource {
13-
return &schema.Resource{
14-
Read: dataSourceCatalogItemEntitlementRead,
15-
Importer: &schema.ResourceImporter{
16-
State: schema.ImportStatePassthrough,
17-
},
11+
return &schema.Resource{
12+
Read: dataSourceCatalogItemEntitlementRead,
13+
Importer: &schema.ResourceImporter{
14+
StateContext: schema.ImportStatePassthroughContext,
15+
},
1816

19-
Schema: map[string]*schema.Schema{
20-
"catalog_item_id": {
21-
Type: schema.TypeString,
22-
Optional: true,
23-
},
24-
"definition": {
25-
Type: schema.TypeSet,
26-
Computed: true,
27-
Elem: &schema.Resource{
28-
Schema: map[string]*schema.Schema{
29-
"description": {
30-
Type: schema.TypeString,
31-
Computed: true,
32-
},
33-
"id": {
34-
Type: schema.TypeString,
35-
Computed: true,
36-
},
37-
"name": {
38-
Type: schema.TypeString,
39-
Computed: true,
40-
},
41-
"source_type": {
42-
Type: schema.TypeString,
43-
Computed: true,
44-
},
45-
"type": {
46-
Type: schema.TypeString,
47-
Computed: true,
48-
},
49-
},
50-
},
51-
},
52-
"id": {
53-
Type: schema.TypeString,
54-
Optional: true,
55-
},
56-
"project_id": {
57-
Type: schema.TypeString,
58-
Required: true,
59-
},
60-
},
61-
}
17+
Schema: map[string]*schema.Schema{
18+
"catalog_item_id": {
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Description: "Catalog item id.",
22+
ConflictsWith: []string{"id"},
23+
},
24+
"definition": {
25+
Type: schema.TypeSet,
26+
Computed: true,
27+
Elem: &schema.Resource{
28+
Schema: map[string]*schema.Schema{
29+
"description": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
Description: "Description of the catalog item.",
33+
},
34+
"icon_id": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
Description: "Icon id of associated catalog item.",
38+
},
39+
"id": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
Description: "Id of the catalog item.",
43+
},
44+
"name": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: "Name of the catalog item.",
48+
},
49+
"number_of_items": {
50+
Type: schema.TypeInt,
51+
Computed: true,
52+
Description: "Number of items in the associated catalog source.",
53+
},
54+
"source_name": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: "Catalog source name.",
58+
},
59+
"source_type": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "Catalog source type.",
63+
},
64+
"type": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
Description: "Content definition type.",
68+
},
69+
},
70+
},
71+
},
72+
"id": {
73+
Type: schema.TypeString,
74+
Optional: true,
75+
Description: "Entitlement id.",
76+
ConflictsWith: []string{"catalog_item_id"},
77+
},
78+
"project_id": {
79+
Type: schema.TypeString,
80+
Required: true,
81+
Description: "Project id.",
82+
},
83+
},
84+
}
6285
}
6386

6487
func dataSourceCatalogItemEntitlementRead(d *schema.ResourceData, m interface{}) error {
65-
log.Printf("Reading the vra_catalog_item_entitlement data source")
66-
apiClient := m.(*Client).apiClient
88+
apiClient := m.(*Client).apiClient
6789

68-
id, idOk := d.GetOk("id")
69-
catalogItemID, catalogItemIDOk := d.GetOk("catalog_item_id")
70-
projectID := d.Get("project_id").(string)
90+
id, idOk := d.GetOk("id")
91+
catalogItemID, catalogItemIDOk := d.GetOk("catalog_item_id")
92+
projectID := d.Get("project_id").(string)
7193

72-
if !idOk && !catalogItemIDOk {
73-
return fmt.Errorf("one of id or catalog_item_id must be provided with project_id")
74-
}
94+
if !idOk && !catalogItemIDOk {
95+
return fmt.Errorf("one of id or catalog_item_id must be provided with project_id")
96+
}
7597

76-
resp, err := apiClient.CatalogEntitlements.GetEntitlementsUsingGET(
77-
catalog_entitlements.NewGetEntitlementsUsingGETParams().WithProjectID(withString(projectID)))
98+
resp, err := apiClient.CatalogEntitlements.GetEntitlementsUsingGET2(
99+
catalog_entitlements.NewGetEntitlementsUsingGET2Params().WithProjectID(withString(projectID)))
100+
if err != nil {
101+
return err
102+
}
78103

79-
if err != nil {
80-
return err
81-
}
82-
83-
setFields := func(entitlement *models.Entitlement) {
84-
d.SetId(entitlement.ID.String())
85-
d.Set("project_id", entitlement.ProjectID)
86-
d.Set("catalog_item_id", entitlement.Definition.ID)
87-
d.Set("definition", flattenContentDefinition(entitlement.Definition))
88-
}
89-
90-
if len(resp.Payload) > 0 {
91-
for _, entitlement := range resp.Payload {
92-
if idOk && entitlement.ID.String() == id.(string) {
93-
setFields(entitlement)
94-
log.Printf("Finished reading the vra_catalog_item_entitlement data item")
95-
return nil
96-
}
97-
98-
if catalogItemIDOk && entitlement.Definition.ID.String() == catalogItemID.(string) {
99-
setFields(entitlement)
100-
log.Printf("Finished reading the vra_catalog_item_entitlement data item")
101-
return nil
102-
}
103-
}
104-
}
105-
106-
return fmt.Errorf("no catalog item entitlements found for the project_id '%v'", projectID)
104+
if len(resp.Payload) > 0 {
105+
for _, entitlement := range resp.Payload {
106+
if (idOk && entitlement.ID.String() == id.(string)) || (catalogItemIDOk && entitlement.Definition.ID.String() == catalogItemID.(string)) {
107+
d.SetId(entitlement.ID.String())
108+
d.Set("catalog_item_id", entitlement.Definition.ID)
109+
d.Set("definition", flattenContentDefinition(entitlement.Definition))
110+
d.Set("project_id", entitlement.ProjectID)
111+
return nil
112+
}
113+
}
114+
}
107115

116+
return fmt.Errorf("no catalog item entitlements found for the project_id '%s'", projectID)
108117
}

vra/data_source_catalog_item_entitlement_test.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
package vra
22

33
import (
4-
"fmt"
5-
"testing"
4+
"fmt"
5+
"testing"
66

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
99
)
1010

1111
func TestAccDataSourceVRACatalogItemEntitlement_Valid(t *testing.T) {
12-
rInt := acctest.RandInt()
13-
resource1 := "vra_catalog_item_entitlement.this"
14-
dataSource1 := "data.vra_catalog_item_entitlement.with_id"
15-
dataSource2 := "data.vra_catalog_item_entitlement.with_catalog_item_id"
16-
catalogSource := "vra_catalog_source_blueprint.this"
17-
project := "vra_project.this"
12+
rInt := acctest.RandInt()
13+
resource1 := "vra_catalog_item_entitlement.this"
14+
dataSource1 := "data.vra_catalog_item_entitlement.with_id"
15+
dataSource2 := "data.vra_catalog_item_entitlement.with_catalog_item_id"
16+
catalogSource := "vra_catalog_item_blueprint.this"
17+
project := "vra_project.this"
1818

19-
resource.Test(t, resource.TestCase{
20-
PreCheck: func() { testAccPreCheckBlueprint(t) },
21-
Providers: testAccProviders,
22-
Steps: []resource.TestStep{
23-
{
24-
Config: testAccCheckVRACatalogSourceEntitlementFoundConfig(rInt),
25-
Check: resource.ComposeAggregateTestCheckFunc(
26-
resource.TestCheckResourceAttrPair(resource1, "id", dataSource1, "id"),
27-
resource.TestCheckResourceAttrPair(resource1, "definition.0.id", dataSource1, "definition.0.id"),
28-
resource.TestCheckResourceAttrPair(catalogSource, "id", dataSource1, "definition.0.id"),
29-
resource.TestCheckResourceAttrPair(catalogSource, "name", dataSource1, "definition.0.name"),
30-
resource.TestCheckResourceAttrPair(project, "id", dataSource1, "project_id"),
31-
resource.TestCheckResourceAttrPair(resource1, "id", dataSource2, "id"),
32-
resource.TestCheckResourceAttrPair(resource1, "definition.0.id", dataSource2, "definition.0.id"),
33-
resource.TestCheckResourceAttrPair(catalogSource, "id", dataSource2, "definition.0.id"),
34-
resource.TestCheckResourceAttrPair(catalogSource, "name", dataSource2, "definition.0.name"),
35-
resource.TestCheckResourceAttrPair(project, "id", dataSource2, "project_id"),
36-
),
37-
},
38-
},
39-
})
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheckBlueprint(t) },
21+
Providers: testAccProviders,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccCheckVRACatalogItemEntitlementFoundConfig(rInt),
25+
Check: resource.ComposeAggregateTestCheckFunc(
26+
resource.TestCheckResourceAttrPair(resource1, "id", dataSource1, "id"),
27+
resource.TestCheckResourceAttrPair(resource1, "definition.0.id", dataSource1, "definition.0.id"),
28+
resource.TestCheckResourceAttrPair(catalogSource, "id", dataSource1, "definition.0.id"),
29+
resource.TestCheckResourceAttrPair(catalogSource, "name", dataSource1, "definition.0.name"),
30+
resource.TestCheckResourceAttrPair(project, "id", dataSource1, "project_id"),
31+
resource.TestCheckResourceAttrPair(resource1, "id", dataSource2, "id"),
32+
resource.TestCheckResourceAttrPair(resource1, "definition.0.id", dataSource2, "definition.0.id"),
33+
resource.TestCheckResourceAttrPair(catalogSource, "id", dataSource2, "definition.0.id"),
34+
resource.TestCheckResourceAttrPair(catalogSource, "name", dataSource2, "definition.0.name"),
35+
resource.TestCheckResourceAttrPair(project, "id", dataSource2, "project_id"),
36+
),
37+
},
38+
},
39+
})
4040
}
4141

4242
func testAccCheckVRACatalogItemEntitlementFoundConfig(rInt int) string {
43-
return fmt.Sprintf(`
43+
return fmt.Sprintf(`
4444
resource "vra_project" "this" {
4545
name = "tf-test-project-%d"
4646
}

vra/data_source_catalog_source_entitlement.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,69 @@ func dataSourceCatalogSourceEntitlement() *schema.Resource {
1818

1919
Schema: map[string]*schema.Schema{
2020
"catalog_source_id": {
21-
Type: schema.TypeString,
22-
Optional: true,
21+
Type: schema.TypeString,
22+
Optional: true,
23+
Description: "Catalog source id.",
24+
ConflictsWith: []string{"id"},
2325
},
2426
"definition": {
2527
Type: schema.TypeSet,
2628
Computed: true,
2729
Elem: &schema.Resource{
2830
Schema: map[string]*schema.Schema{
2931
"description": {
30-
Type: schema.TypeString,
31-
Computed: true,
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "Description of the catalog source.",
35+
},
36+
"icon_id": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
Description: "Icon id of associated catalog source.",
3240
},
3341
"id": {
34-
Type: schema.TypeString,
35-
Computed: true,
42+
Type: schema.TypeString,
43+
Computed: true,
44+
Description: "Id of the catalog source.",
3645
},
3746
"name": {
38-
Type: schema.TypeString,
39-
Computed: true,
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: "Name of the catalog source.",
4050
},
4151
"number_of_items": {
42-
Type: schema.TypeInt,
43-
Computed: true,
52+
Type: schema.TypeInt,
53+
Computed: true,
54+
Description: "Number of items in the associated catalog source.",
55+
},
56+
"source_name": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
Description: "Catalog source name.",
4460
},
4561
"source_type": {
46-
Type: schema.TypeString,
47-
Computed: true,
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "Catalog source type.",
4865
},
4966
"type": {
50-
Type: schema.TypeString,
51-
Computed: true,
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Content definition type.",
5270
},
5371
},
5472
},
5573
},
5674
"id": {
57-
Type: schema.TypeString,
58-
Optional: true,
75+
Type: schema.TypeString,
76+
Optional: true,
77+
Description: "Entitlement id.",
78+
ConflictsWith: []string{"catalog_source_id"},
5979
},
6080
"project_id": {
61-
Type: schema.TypeString,
62-
Required: true,
81+
Type: schema.TypeString,
82+
Required: true,
83+
Description: "Project id.",
6384
},
6485
},
6586
}

vra/provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func Provider() *schema.Provider {
5050
"vra_blueprint": dataSourceBlueprint(),
5151
"vra_blueprint_version": dataSourceBlueprintVersion(),
5252
"vra_catalog_item": dataSourceCatalogItem(),
53+
"vra_catalog_item_entitlement": dataSourceCatalogItemEntitlement(),
5354
"vra_catalog_source_blueprint": dataSourceCatalogSourceBlueprint(),
5455
"vra_catalog_source_entitlement": dataSourceCatalogSourceEntitlement(),
55-
"vra_catalog_item_entitlement": dataSourceCatalogItemEntitlement(),
5656
"vra_cloud_account_aws": dataSourceCloudAccountAWS(),
5757
"vra_cloud_account_azure": dataSourceCloudAccountAzure(),
5858
"vra_cloud_account_gcp": dataSourceCloudAccountGCP(),
@@ -94,9 +94,9 @@ func Provider() *schema.Provider {
9494
"vra_block_device_snapshot": resourceBlockDeviceSnapshot(),
9595
"vra_blueprint": resourceBlueprint(),
9696
"vra_blueprint_version": resourceBlueprintVersion(),
97+
"vra_catalog_item_entitlement": resourceCatalogItemEntitlement(),
9798
"vra_catalog_source_blueprint": resourceCatalogSourceBlueprint(),
9899
"vra_catalog_source_entitlement": resourceCatalogSourceEntitlement(),
99-
"vra_catalog_item_entitlement": resourceCatalogItemEntitlement(),
100100
"vra_cloud_account_aws": resourceCloudAccountAWS(),
101101
"vra_cloud_account_azure": resourceCloudAccountAzure(),
102102
"vra_cloud_account_gcp": resourceCloudAccountGCP(),

0 commit comments

Comments
 (0)