Skip to content

Commit cf4caa4

Browse files
Richard Gullovarmax2511
authored andcommitted
Added Support For - Standard Tags
1 parent 03ec91a commit cf4caa4

18 files changed

+874
-2
lines changed

examples/identity/availability_domains.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ output "ads" {
1616

1717
data "oci_identity_availability_domain" "ad" {
1818
compartment_id = var.tenancy_ocid
19-
ad_number = 2
19+
ad_number = 1
2020
}
2121

2222
output "ad" {

examples/identity/group.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ output "groups" {
3535
*/
3636

3737
variable "dynamic_group_defined_tags_value" {
38-
default = "value"
38+
default = "test_value"
3939
}
4040

4141
variable "dynamic_group_freeform_tags" {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
variable "test_tag_namespace_name" {
5+
default = "Oracle-Standard"
6+
}
7+
8+
resource "oci_identity_import_standard_tags_management" "test_import_standard_tags_management" {
9+
#Required
10+
compartment_id = var.compartment_id
11+
standard_tag_namespace_name = var.test_tag_namespace_name
12+
}
13+
14+

examples/identity/networksources.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
22
// Licensed under the Mozilla Public License v2.0
33
variable "compartment_id" {
4+
45
}
56

67
variable "network_source_defined_tags_value" {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
5+
data "oci_identity_tag_standard_tag_namespace_template" "test_tag_standard_tag_namespace_template" {
6+
#Required
7+
compartment_id = var.compartment_id
8+
standard_tag_namespace_name = var.test_tag_namespace_name
9+
}
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
5+
data "oci_identity_tag_standard_tag_namespace_templates" "test_tag_standard_tag_namespace_template" {
6+
#Required
7+
compartment_id = var.compartment_id
8+
}
9+

examples/identity/user.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
resource "oci_identity_user" "user1" {
99
name = "tf-example-user"
10+
1011
description = "user created by terraform"
1112
compartment_id = var.tenancy_ocid
1213
}
1314

1415
// Use the "user2" to have non-default values of capabilities without corresponding authentication resources being actually created
1516
resource "oci_identity_user" "user2" {
1617
name = "tf-example-user2"
18+
1719
description = "user2 created by terraform"
1820
compartment_id = var.tenancy_ocid
1921
}

oci/export_definitions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,11 @@ var exportIdentityTagHints = &TerraformResourceHints{
19831983
},
19841984
}
19851985

1986+
var exportIdentityImportStandardTagsManagementHints = &TerraformResourceHints{
1987+
resourceClass: "oci_identity_import_standard_tags_management",
1988+
resourceAbbreviation: "import_standard_tags_management",
1989+
}
1990+
19861991
var exportIdentityDomainHints = &TerraformResourceHints{
19871992
resourceClass: "oci_identity_domain",
19881993
datasourceClass: "oci_identity_domains",
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package oci
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"strings"
10+
"time"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
13+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
14+
15+
oci_common "github.com/oracle/oci-go-sdk/v54/common"
16+
oci_identity "github.com/oracle/oci-go-sdk/v54/identity"
17+
)
18+
19+
func init() {
20+
RegisterResource("oci_identity_import_standard_tags_management", IdentityImportStandardTagsManagementResource())
21+
}
22+
23+
func IdentityImportStandardTagsManagementResource() *schema.Resource {
24+
return &schema.Resource{
25+
Importer: &schema.ResourceImporter{
26+
State: schema.ImportStatePassthrough,
27+
},
28+
Timeouts: DefaultTimeout,
29+
Create: createIdentityImportStandardTagsManagement,
30+
Read: readIdentityImportStandardTagsManagement,
31+
Delete: deleteIdentityImportStandardTagsManagement,
32+
Schema: map[string]*schema.Schema{
33+
// Required
34+
"compartment_id": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
ForceNew: true,
38+
},
39+
"standard_tag_namespace_name": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
ForceNew: true,
43+
},
44+
45+
// Optional
46+
47+
// Computed
48+
"work_request_id": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
},
53+
}
54+
}
55+
56+
func createIdentityImportStandardTagsManagement(d *schema.ResourceData, m interface{}) error {
57+
sync := &IdentityImportStandardTagsManagementResourceCrud{}
58+
sync.D = d
59+
sync.Client = m.(*OracleClients).identityClient()
60+
61+
return CreateResource(d, sync)
62+
}
63+
64+
func readIdentityImportStandardTagsManagement(d *schema.ResourceData, m interface{}) error {
65+
return nil
66+
}
67+
68+
func deleteIdentityImportStandardTagsManagement(d *schema.ResourceData, m interface{}) error {
69+
return nil
70+
}
71+
72+
type IdentityImportStandardTagsManagementResourceCrud struct {
73+
BaseCrud
74+
Client *oci_identity.IdentityClient
75+
Res *oci_identity.ImportStandardTagsResponse
76+
DisableNotFoundRetries bool
77+
}
78+
79+
func (s *IdentityImportStandardTagsManagementResourceCrud) ID() string {
80+
return *s.Res.OpcWorkRequestId
81+
}
82+
83+
func (s *IdentityImportStandardTagsManagementResourceCrud) Create() error {
84+
request := oci_identity.ImportStandardTagsRequest{}
85+
86+
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
87+
tmp := compartmentId.(string)
88+
request.CompartmentId = &tmp
89+
}
90+
91+
if standardTagNamespaceName, ok := s.D.GetOkExists("standard_tag_namespace_name"); ok {
92+
tmp := standardTagNamespaceName.(string)
93+
request.StandardTagNamespaceName = &tmp
94+
}
95+
96+
request.RequestMetadata.RetryPolicy = GetRetryPolicy(s.DisableNotFoundRetries, "identity")
97+
98+
response, err := s.Client.ImportStandardTags(context.Background(), request)
99+
if err != nil {
100+
return err
101+
}
102+
103+
workId := response.OpcWorkRequestId
104+
s.D.SetId(*workId)
105+
106+
s.Res = &response
107+
108+
return s.getImportStandardTagsManagementFromWorkRequest(workId, GetRetryPolicy(s.DisableNotFoundRetries, "identity"), oci_identity.WorkRequestResourceActionTypeCreated, s.D.Timeout(schema.TimeoutCreate))
109+
}
110+
111+
func (s *IdentityImportStandardTagsManagementResourceCrud) getImportStandardTagsManagementFromWorkRequest(workId *string, retryPolicy *oci_common.RetryPolicy,
112+
actionTypeEnum oci_identity.WorkRequestResourceActionTypeEnum, timeout time.Duration) error {
113+
114+
// Wait until it finishes
115+
_, err := importStandardTagsManagementWaitForWorkRequest(workId, "identity",
116+
actionTypeEnum, timeout, s.DisableNotFoundRetries, s.Client)
117+
118+
if err != nil {
119+
return err
120+
}
121+
122+
s.D.SetId(*workId)
123+
124+
return nil
125+
}
126+
127+
func importStandardTagsManagementWorkRequestShouldRetryFunc(timeout time.Duration) func(response oci_common.OCIOperationResponse) bool {
128+
startTime := time.Now()
129+
stopTime := startTime.Add(timeout)
130+
return func(response oci_common.OCIOperationResponse) bool {
131+
132+
// Stop after timeout has elapsed
133+
if time.Now().After(stopTime) {
134+
return false
135+
}
136+
137+
// Make sure we stop on default rules
138+
if shouldRetry(response, false, "identity", startTime) {
139+
return true
140+
}
141+
142+
// Only stop if the time Finished is set
143+
if workRequestResponse, ok := response.Response.(oci_identity.GetTaggingWorkRequestResponse); ok {
144+
return workRequestResponse.TimeFinished == nil
145+
}
146+
return false
147+
}
148+
}
149+
150+
func importStandardTagsManagementWaitForWorkRequest(wId *string, entityType string, action oci_identity.WorkRequestResourceActionTypeEnum,
151+
timeout time.Duration, disableFoundRetries bool, client *oci_identity.IdentityClient) (*string, error) {
152+
retryPolicy := GetRetryPolicy(disableFoundRetries, "identity")
153+
retryPolicy.ShouldRetryOperation = importStandardTagsManagementWorkRequestShouldRetryFunc(timeout)
154+
155+
response := oci_identity.GetTaggingWorkRequestResponse{}
156+
stateConf := &resource.StateChangeConf{
157+
Pending: []string{
158+
string(oci_identity.TaggingWorkRequestStatusInProgress),
159+
string(oci_identity.TaggingWorkRequestStatusAccepted),
160+
string(oci_identity.TaggingWorkRequestStatusCanceling),
161+
},
162+
Target: []string{
163+
string(oci_identity.TaggingWorkRequestStatusSucceeded),
164+
string(oci_identity.TaggingWorkRequestStatusFailed),
165+
string(oci_identity.TaggingWorkRequestStatusCanceled),
166+
},
167+
Refresh: func() (interface{}, string, error) {
168+
var err error
169+
response, err = client.GetTaggingWorkRequest(context.Background(),
170+
oci_identity.GetTaggingWorkRequestRequest{
171+
WorkRequestId: wId,
172+
RequestMetadata: oci_common.RequestMetadata{
173+
RetryPolicy: retryPolicy,
174+
},
175+
})
176+
wr := &response.TaggingWorkRequest
177+
return wr, string(wr.Status), err
178+
},
179+
Timeout: timeout,
180+
}
181+
if _, e := stateConf.WaitForState(); e != nil {
182+
return nil, e
183+
}
184+
185+
// The work request response contains an array of resources, but that array will always be empty
186+
// Hence, no handling of this array is necessary (unlike in other Terraform managed resources)
187+
188+
// The workrequest may have failed - so check for failed or canceled work requests
189+
if response.Status == oci_identity.TaggingWorkRequestStatusFailed || response.Status == oci_identity.TaggingWorkRequestStatusCanceled {
190+
return nil, getErrorFromIdentityImportStandardTagsManagementWorkRequest(client, wId, retryPolicy, entityType, action)
191+
}
192+
193+
return wId, nil
194+
195+
}
196+
197+
func getErrorFromIdentityImportStandardTagsManagementWorkRequest(client *oci_identity.IdentityClient, workId *string, retryPolicy *oci_common.RetryPolicy, entityType string, action oci_identity.WorkRequestResourceActionTypeEnum) error {
198+
response, err := client.ListTaggingWorkRequestErrors(context.Background(),
199+
oci_identity.ListTaggingWorkRequestErrorsRequest{
200+
WorkRequestId: workId,
201+
RequestMetadata: oci_common.RequestMetadata{
202+
RetryPolicy: retryPolicy,
203+
},
204+
})
205+
if err != nil {
206+
return err
207+
}
208+
209+
allErrs := make([]string, 0)
210+
for _, wrkErr := range response.Items {
211+
allErrs = append(allErrs, *wrkErr.Message)
212+
}
213+
errorMessage := strings.Join(allErrs, "\n")
214+
215+
workRequestErr := fmt.Errorf("work request did not succeed, workId: %s, entity: %s, action: %s. Message: %s", *workId, entityType, action, errorMessage)
216+
217+
return workRequestErr
218+
}
219+
220+
func (s *IdentityImportStandardTagsManagementResourceCrud) SetData() error {
221+
if s.Res.OpcWorkRequestId != nil {
222+
s.D.Set("work_request_id", *s.Res.OpcWorkRequestId)
223+
}
224+
225+
return nil
226+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package oci
5+
6+
import (
7+
"fmt"
8+
"strconv"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
13+
14+
"github.com/terraform-providers/terraform-provider-oci/httpreplay"
15+
)
16+
17+
var (
18+
importStandardTagsManagementRepresentation = map[string]interface{}{
19+
"compartment_id": Representation{RepType: Required, Create: `${var.compartment_id}`},
20+
"standard_tag_namespace_name": Representation{RepType: Required, Create: `${oci_identity_tag_namespace.test_tag_namespace.name}`},
21+
}
22+
23+
standardTagNamespaceRepresentation = map[string]interface{}{
24+
"compartment_id": Representation{RepType: Required, Create: `${var.compartment_id}`},
25+
"description": Representation{RepType: Required, Create: `Oracle recommended tags`},
26+
"name": Representation{RepType: Required, Create: `Oracle-Standard`},
27+
}
28+
29+
ImportStandardTagsManagementResourceDependencies = GenerateResourceFromRepresentationMap("oci_identity_tag_namespace", "test_tag_namespace", Required, Create, standardTagNamespaceRepresentation)
30+
)
31+
32+
// issue-routing-tag: identity/default
33+
func TestIdentityImportStandardTagsManagementResource_basic(t *testing.T) {
34+
httpreplay.SetScenario("TestIdentityImportStandardTagsManagementResource_basic")
35+
defer httpreplay.SaveScenario()
36+
37+
config := testProviderConfig()
38+
39+
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
40+
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
41+
42+
resourceName := "oci_identity_import_standard_tags_management.test_import_standard_tags_management"
43+
44+
var resId string
45+
// Save TF content to Create resource with only required properties. This has to be exactly the same as the config part in the create step in the test.
46+
SaveConfigContent(config+compartmentIdVariableStr+ImportStandardTagsManagementResourceDependencies+
47+
GenerateResourceFromRepresentationMap("oci_identity_import_standard_tags_management", "test_import_standard_tags_management", Required, Create, importStandardTagsManagementRepresentation), "identity", "importStandardTagsManagement", t)
48+
49+
ResourceTest(t, nil, []resource.TestStep{
50+
// verify Create
51+
{
52+
Config: config + compartmentIdVariableStr + ImportStandardTagsManagementResourceDependencies +
53+
GenerateResourceFromRepresentationMap("oci_identity_import_standard_tags_management", "test_import_standard_tags_management", Required, Create, importStandardTagsManagementRepresentation),
54+
Check: ComposeAggregateTestCheckFuncWrapper(
55+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
56+
resource.TestCheckResourceAttrSet(resourceName, "standard_tag_namespace_name"),
57+
58+
func(s *terraform.State) (err error) {
59+
resId, err = FromInstanceState(s, resourceName, "id")
60+
if isEnableExportCompartment, _ := strconv.ParseBool(getEnvSettingWithDefault("enable_export_compartment", "true")); isEnableExportCompartment {
61+
if errExport := TestExportCompartmentWithResourceName(&resId, &compartmentId, resourceName); errExport != nil {
62+
return errExport
63+
}
64+
}
65+
return err
66+
},
67+
),
68+
},
69+
})
70+
}

0 commit comments

Comments
 (0)