|
| 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 | +} |
0 commit comments