Skip to content

Commit ac130ef

Browse files
committed
Add vra_content_sharing_policy data source and resource
* Add vra_content_sharing_policy data source and resource * Deprecate vra_catalog_item_entitlement datasource and resource * Deprecate vra_catalog_source_entitlement datasource and resource Signed-off-by: Ferran Rodenas <[email protected]>
1 parent b46cd39 commit ac130ef

20 files changed

+868
-13
lines changed

.golangci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ linters:
2525
- typecheck
2626
- unconvert
2727
- unused
28-
- vet
29-
- vetshadow
28+
- govet
3029

3130
linters-settings:
3231
errcheck:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/go-openapi/runtime v0.25.0
77
github.com/go-openapi/strfmt v0.21.3
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.25.0
9-
github.com/vmware/vra-sdk-go v0.6.0
9+
github.com/vmware/vra-sdk-go v0.6.1
1010
)
1111

1212
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+
302302
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
303303
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
304304
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
305-
github.com/vmware/vra-sdk-go v0.6.0 h1:yQfF67UrFFWyHYcdLGK9Up710kGM57QXGLAM2j/tmtw=
306-
github.com/vmware/vra-sdk-go v0.6.0/go.mod h1:gUj5Z/+qvXEDk9+0NvW0ynDrMkTk3P5Bdt2JfN+AAC8=
305+
github.com/vmware/vra-sdk-go v0.6.1 h1:2DscXG18be7MknOX1zIZFekB/NQV3eJyH1dEkX1E0+4=
306+
github.com/vmware/vra-sdk-go v0.6.1/go.mod h1:gUj5Z/+qvXEDk9+0NvW0ynDrMkTk3P5Bdt2JfN+AAC8=
307307
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
308308
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
309309
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=

vra/data_source_catalog_item_entitlement.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99

1010
func dataSourceCatalogItemEntitlement() *schema.Resource {
1111
return &schema.Resource{
12-
Read: dataSourceCatalogItemEntitlementRead,
12+
DeprecationMessage: "'vra_catalog_item_entitlement' is deprecated. Use 'vra_content_sharing_policy' instead.",
13+
Read: dataSourceCatalogItemEntitlementRead,
1314
Importer: &schema.ResourceImporter{
1415
StateContext: schema.ImportStatePassthroughContext,
1516
},

vra/data_source_catalog_source_entitlement.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111

1212
func dataSourceCatalogSourceEntitlement() *schema.Resource {
1313
return &schema.Resource{
14-
Read: dataSourceCatalogSourceEntitlementRead,
14+
DeprecationMessage: "'vra_catalog_source_entitlement' is deprecated. Use 'vra_content_sharing_policy' instead.",
15+
Read: dataSourceCatalogSourceEntitlementRead,
1516
Importer: &schema.ResourceImporter{
1617
StateContext: schema.ImportStatePassthroughContext,
1718
},
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package vra
2+
3+
import (
4+
"context"
5+
6+
"github.com/go-openapi/strfmt"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/vmware/vra-sdk-go/pkg/client/policies"
10+
)
11+
12+
func dataSourceContentSharingPolicy() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceContentSharingPolicyRead,
15+
Importer: &schema.ResourceImporter{
16+
StateContext: schema.ImportStatePassthroughContext,
17+
},
18+
Schema: map[string]*schema.Schema{
19+
// Input attributes
20+
"id": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
Computed: true,
24+
Description: "The policy ID.",
25+
ConflictsWith: []string{"name"},
26+
},
27+
"name": {
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Computed: true,
31+
Description: "The policy name.",
32+
ConflictsWith: []string{"id"},
33+
},
34+
35+
// Computed attributes
36+
"catalog_item_ids": {
37+
Type: schema.TypeSet,
38+
Computed: true,
39+
Description: "List of catalog item ids to share.",
40+
Elem: &schema.Schema{
41+
Type: schema.TypeString,
42+
},
43+
},
44+
"catalog_source_ids": {
45+
Type: schema.TypeSet,
46+
Computed: true,
47+
Description: "List of catalog source ids to share.",
48+
Elem: &schema.Schema{
49+
Type: schema.TypeString,
50+
},
51+
},
52+
"created_at": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
Description: "Policy creation timestamp.",
56+
},
57+
"created_by": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: "Policy author.",
61+
},
62+
"description": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
Description: "The policy description.",
66+
},
67+
"last_updated_at": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
Description: "Most recent policy update timestamp.",
71+
},
72+
"last_updated_by": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
Description: "Most recent policy editor.",
76+
},
77+
"org_id": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Description: "The ID of the organization to which the policy belongs.",
81+
},
82+
"project_id": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
Description: "The ID of the project to which the policy belongs.",
86+
},
87+
},
88+
}
89+
}
90+
91+
func dataSourceContentSharingPolicyRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
92+
apiClient := m.(*Client).apiClient
93+
94+
id, idOk := d.GetOk("id")
95+
name, nameOk := d.GetOk("name")
96+
97+
if !idOk && !nameOk {
98+
return diag.Errorf("one of id or name must be provided")
99+
}
100+
101+
if nameOk {
102+
resp, err := apiClient.Policies.GetPoliciesUsingGET5(policies.NewGetPoliciesUsingGET5Params())
103+
if err != nil {
104+
return diag.FromErr(err)
105+
}
106+
for _, policy := range resp.GetPayload().Content {
107+
if policy.Name == name {
108+
id = policy.ID.String()
109+
break
110+
}
111+
}
112+
if id == "" {
113+
return diag.Errorf("content sharing policy with name '%s' not found", name)
114+
}
115+
}
116+
117+
resp, err := apiClient.Policies.GetPolicyUsingGET5(policies.NewGetPolicyUsingGET5Params().WithID(strfmt.UUID(id.(string))))
118+
if err != nil {
119+
switch err.(type) {
120+
case *policies.GetPolicyUsingGET5NotFound:
121+
return diag.Errorf("content sharing policy with id '%s' not found", id)
122+
default:
123+
// nop
124+
}
125+
return diag.FromErr(err)
126+
}
127+
128+
policy := resp.GetPayload()
129+
d.SetId(string(policy.ID))
130+
d.Set("name", policy.Name)
131+
d.Set("created_at", policy.CreatedAt.String())
132+
d.Set("created_by", policy.CreatedBy)
133+
d.Set("description", policy.Description)
134+
d.Set("last_updated_at", policy.LastUpdatedAt.String())
135+
d.Set("last_updated_by", policy.LastUpdatedBy)
136+
d.Set("org_id", policy.OrgID)
137+
d.Set("project_id", policy.ProjectID)
138+
139+
catalogItemIDs, err := extractCatalogItemIDsFromContentSharingPolicy(policy.Definition)
140+
if err != nil {
141+
return diag.Errorf("error extracting catalog item ids from content sharing policy: %s", err.Error())
142+
}
143+
144+
if err := d.Set("catalog_item_ids", catalogItemIDs); err != nil {
145+
return diag.Errorf("error setting catalog_item_ids: %s", err.Error())
146+
}
147+
148+
catalogSourceIDs, err := extractCatalogSourceIDsFromContentSharingPolicy(policy.Definition)
149+
if err != nil {
150+
return diag.Errorf("error extracting catalog source ids from content sharing policy: %s", err.Error())
151+
}
152+
153+
if err := d.Set("catalog_source_ids", catalogSourceIDs); err != nil {
154+
return diag.Errorf("error setting catalog_source_ids: %s", err.Error())
155+
}
156+
157+
return nil
158+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package vra
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
10+
"testing"
11+
)
12+
13+
func TestAccDataSourceVRAContentSharingPolicy(t *testing.T) {
14+
rInt := acctest.RandInt()
15+
resourceName := "vra_content_sharing_policy.test"
16+
dataSourceName := "data.vra_content_sharing_policy.test"
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheckContentSharingPolicy(t) },
20+
Providers: testAccProviders,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccDataSourceVRAContentSharingPolicyByID(rInt),
24+
Check: resource.ComposeTestCheckFunc(
25+
resource.TestCheckResourceAttrPair(
26+
resourceName, "name", dataSourceName, "name"),
27+
resource.TestCheckResourceAttrPair(
28+
resourceName, "description", dataSourceName, "description"),
29+
resource.TestCheckResourceAttrPair(
30+
resourceName, "project_id", dataSourceName, "project_id"),
31+
resource.TestCheckResourceAttrPair(
32+
resourceName, "catalog_item_ids.#", dataSourceName, "catalog_item_ids.#"),
33+
resource.TestCheckTypeSetElemAttrPair(
34+
resourceName, "catalog_item_ids.0", dataSourceName, "catalog_item_ids.0"),
35+
resource.TestCheckResourceAttrPair(
36+
resourceName, "catalog_source_ids.#", dataSourceName, "catalog_source_ids.#"),
37+
resource.TestCheckTypeSetElemAttrPair(
38+
resourceName, "catalog_source_ids.0", dataSourceName, "catalog_source_ids.0"),
39+
),
40+
},
41+
{
42+
Config: testAccDataSourceVRAContentSharingPolicyByName(rInt),
43+
Check: resource.ComposeTestCheckFunc(
44+
resource.TestCheckResourceAttrPair(
45+
resourceName, "name", dataSourceName, "name"),
46+
resource.TestCheckResourceAttrPair(
47+
resourceName, "description", dataSourceName, "description"),
48+
resource.TestCheckResourceAttrPair(
49+
resourceName, "project_id", dataSourceName, "project_id"),
50+
resource.TestCheckResourceAttrPair(
51+
resourceName, "catalog_item_ids.#", dataSourceName, "catalog_item_ids.#"),
52+
resource.TestCheckTypeSetElemAttrPair(
53+
resourceName, "catalog_item_ids.0", dataSourceName, "catalog_item_ids.0"),
54+
resource.TestCheckResourceAttrPair(
55+
resourceName, "catalog_source_ids.#", dataSourceName, "catalog_source_ids.#"),
56+
resource.TestCheckTypeSetElemAttrPair(
57+
resourceName, "catalog_source_ids.0", dataSourceName, "catalog_source_ids.0"),
58+
),
59+
},
60+
},
61+
})
62+
}
63+
64+
func testAccDataSourceVRAContentSharingPolicy(rInt int) string {
65+
projectID := os.Getenv("VRA_PROJECT_ID")
66+
catalogItemID := os.Getenv("VRA_CATALOG_ITEM_ID")
67+
catalogSourceID := os.Getenv("VRA_CATALOG_SOURCE_ID")
68+
return fmt.Sprintf(`
69+
resource "vra_content_sharing_policy" "test" {
70+
name = "content-sharing-policy-%d"
71+
description = "Content Sharing Policy %d"
72+
project_id = "%s"
73+
catalog_item_ids = ["%s"]
74+
catalog_source_ids = ["%s"]
75+
}
76+
`, rInt, rInt, projectID, catalogItemID, catalogSourceID)
77+
}
78+
79+
func testAccDataSourceVRAContentSharingPolicyByID(rInt int) string {
80+
return testAccDataSourceVRAContentSharingPolicy(rInt) + `
81+
data "vra_content_sharing_policy" "test" {
82+
id = vra_content_sharing_policy.test.id
83+
}`
84+
}
85+
86+
func testAccDataSourceVRAContentSharingPolicyByName(rInt int) string {
87+
return testAccDataSourceVRAContentSharingPolicy(rInt) + `
88+
data "vra_content_sharing_policy" "test" {
89+
name = vra_content_sharing_policy.test.name
90+
}`
91+
}

0 commit comments

Comments
 (0)