Skip to content

Commit aec5938

Browse files
committed
Add support for Tag import
1 parent ea75b43 commit aec5938

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
## 3.10.1 (Unreleased)
2+
3+
### Added
4+
- Support for importing tag. Note tag uses custom Id(import only) format (tagNamespaces/{tagNamespaceId}/tags/{tagName}) to support import.
5+
26
## 3.10.0 (December 11, 2018)
37

48
### Added

oci/identity_tag_resource.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ package provider
44

55
import (
66
"context"
7-
8-
"github.com/hashicorp/terraform/helper/schema"
9-
10-
"strings"
11-
7+
"fmt"
8+
"regexp"
129
"strconv"
10+
"strings"
1311

12+
"github.com/hashicorp/terraform/helper/schema"
1413
oci_identity "github.com/oracle/oci-go-sdk/identity"
1514
)
1615

1716
func TagResource() *schema.Resource {
1817
return &schema.Resource{
18+
Importer: &schema.ResourceImporter{
19+
State: schema.ImportStatePassthrough,
20+
},
1921
Timeouts: DefaultTimeout,
2022
Create: createTag,
2123
Read: readTag,
@@ -197,6 +199,12 @@ func (s *TagResourceCrud) Create() error {
197199
func (s *TagResourceCrud) Get() error {
198200
request := oci_identity.GetTagRequest{}
199201

202+
tagName, tagNamespaceId, parseTagCompositeIdErr := parseTagCompositeId(s.D.Id())
203+
if parseTagCompositeIdErr == nil {
204+
request.TagName = &tagName
205+
request.TagNamespaceId = &tagNamespaceId
206+
}
207+
200208
if tagName, ok := s.D.GetOkExists("name"); ok {
201209
tmp := tagName.(string)
202210
request.TagName = &tmp
@@ -215,6 +223,14 @@ func (s *TagResourceCrud) Get() error {
215223
}
216224

217225
s.Res = &response.Tag
226+
if parseTagCompositeIdErr == nil {
227+
// Import sets the ID to composite ID and hence overwriting ID to OCID from response
228+
id := response.Tag.Id
229+
if id == nil {
230+
return fmt.Errorf("error : received null value for id attribute for request %s, id attribute cannot be null", *response.OpcRequestId)
231+
}
232+
s.D.SetId(*id)
233+
}
218234
return nil
219235
}
220236

@@ -302,3 +318,16 @@ func (s *TagResourceCrud) SetData() error {
302318

303319
return nil
304320
}
321+
322+
func parseTagCompositeId(compositeId string) (tagName string, tagNamespaceId string, err error) {
323+
parts := strings.Split(compositeId, "/")
324+
match, _ := regexp.MatchString("tagNamespaces/.*/tags/.*", compositeId)
325+
if !match || len(parts) != 4 {
326+
err = fmt.Errorf("illegal compositeId %s encountered", compositeId)
327+
return
328+
}
329+
tagNamespaceId = parts[1]
330+
tagName = parts[3]
331+
332+
return
333+
}

oci/identity_tag_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ func TestIdentityTagResource_basic(t *testing.T) {
161161
resource.TestCheckResourceAttrSet(datasourceName, "tags.0.time_created"),
162162
),
163163
},
164+
// verify resource import
165+
{
166+
Config: config,
167+
ImportStateIdFunc: getTagCompositeId(resourceName),
168+
ImportState: true,
169+
ImportStateVerify: true,
170+
ImportStateVerifyIgnore: []string{},
171+
ResourceName: resourceName,
172+
},
164173
},
165174
})
166175
}
176+
177+
func getTagCompositeId(resourceName string) resource.ImportStateIdFunc {
178+
return func(s *terraform.State) (string, error) {
179+
rs, ok := s.RootModule().Resources[resourceName]
180+
if !ok {
181+
return "", fmt.Errorf("Not found: %s", resourceName)
182+
}
183+
184+
return fmt.Sprintf("tagNamespaces/%s/tags/%s", rs.Primary.Attributes["tag_namespace_id"], rs.Primary.Attributes["name"]), nil
185+
}
186+
}

website/docs/r/identity_tag.html.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,11 @@ The following attributes are exported:
7070
* `tag_namespace_id` - The OCID of the namespace that contains the tag definition.
7171
* `time_created` - Date and time the tag was created, in the format defined by RFC3339. Example: `2016-08-25T21:10:29.600Z`
7272

73+
## Import
74+
75+
Tags can be imported using the `tagNamespaceId` and `tagName`, e.g.
76+
77+
```
78+
$ terraform import oci_identity_tag.test_tag "tagNamespaces/{tagNamespaceId}/tags/{tagName}"
79+
```
80+

0 commit comments

Comments
 (0)