|
1 | 1 | package vra
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "log" |
| 4 | + "fmt" |
6 | 5 |
|
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" |
10 | 8 | )
|
11 | 9 |
|
12 | 10 | 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 | + }, |
18 | 16 |
|
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 | + } |
62 | 85 | }
|
63 | 86 |
|
64 | 87 | 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 |
67 | 89 |
|
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) |
71 | 93 |
|
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 | + } |
75 | 97 |
|
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 | + } |
78 | 103 |
|
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 | + } |
107 | 115 |
|
| 116 | + return fmt.Errorf("no catalog item entitlements found for the project_id '%s'", projectID) |
108 | 117 | }
|
0 commit comments