33package provider
44
55import (
6+ "context"
67 "fmt"
8+ "strconv"
79 "testing"
10+ "time"
11+
12+ "github.com/oracle/oci-go-sdk/common"
813
914 "github.com/hashicorp/terraform/helper/resource"
15+ "github.com/hashicorp/terraform/helper/schema"
1016 "github.com/hashicorp/terraform/terraform"
17+ oci_identity "github.com/oracle/oci-go-sdk/identity"
1118
1219 "github.com/terraform-providers/terraform-provider-oci/httpreplay"
1320)
1926 tagNamespaceDataSourceRepresentation = map [string ]interface {}{
2027 "compartment_id" : Representation {repType : Required , create : `${var.compartment_id}` },
2128 "include_subcompartments" : Representation {repType : Optional , create : `false` },
29+ "state" : Representation {repType : Optional , create : `AVAILABLE` },
2230 "filter" : RepresentationGroup {Required , tagNamespaceDataSourceFilterRepresentation }}
2331 tagNamespaceDataSourceFilterRepresentation = map [string ]interface {}{
2432 "name" : Representation {repType : Required , create : `id` },
@@ -59,6 +67,7 @@ func TestIdentityTagNamespaceResource_basic(t *testing.T) {
5967 Providers : map [string ]terraform.ResourceProvider {
6068 "oci" : provider ,
6169 },
70+ //CheckDestroy: testAccCheckIdentityTagNamespaceDestroy,
6271 Steps : []resource.TestStep {
6372 // verify create
6473 {
@@ -160,6 +169,7 @@ func TestIdentityTagNamespaceResource_basic(t *testing.T) {
160169 Check : resource .ComposeAggregateTestCheckFunc (
161170 resource .TestCheckResourceAttr (datasourceName , "compartment_id" , compartmentId ),
162171 resource .TestCheckResourceAttr (datasourceName , "include_subcompartments" , "false" ),
172+ resource .TestCheckResourceAttr (datasourceName , "state" , "AVAILABLE" ),
163173
164174 resource .TestCheckResourceAttr (datasourceName , "tag_namespaces.#" , "1" ),
165175 resource .TestCheckResourceAttr (datasourceName , "tag_namespaces.0.compartment_id" , compartmentId ),
@@ -169,6 +179,7 @@ func TestIdentityTagNamespaceResource_basic(t *testing.T) {
169179 resource .TestCheckResourceAttrSet (datasourceName , "tag_namespaces.0.id" ),
170180 resource .TestCheckResourceAttrSet (datasourceName , "tag_namespaces.0.is_retired" ),
171181 resource .TestCheckResourceAttr (datasourceName , "tag_namespaces.0.name" , "BillingTags" ),
182+ resource .TestCheckResourceAttrSet (datasourceName , "tag_namespaces.0.state" ),
172183 resource .TestCheckResourceAttrSet (datasourceName , "tag_namespaces.0.time_created" ),
173184 ),
174185 },
@@ -183,3 +194,137 @@ func TestIdentityTagNamespaceResource_basic(t *testing.T) {
183194 },
184195 })
185196}
197+
198+ func testAccCheckIdentityTagNamespaceDestroy (s * terraform.State ) error {
199+ noResourceFound := true
200+ client := testAccProvider .Meta ().(* OracleClients ).identityClient
201+ for _ , rs := range s .RootModule ().Resources {
202+ if rs .Type == "oci_identity_tag_namespace" {
203+ noResourceFound = false
204+ request := oci_identity.GetTagNamespaceRequest {}
205+
206+ tmp := rs .Primary .ID
207+ request .TagNamespaceId = & tmp
208+
209+ request .RequestMetadata .RetryPolicy = getRetryPolicy (true , "identity" )
210+
211+ response , err := client .GetTagNamespace (context .Background (), request )
212+
213+ if err == nil {
214+ deletedLifecycleStates := map [string ]bool {
215+ string (oci_identity .TagNamespaceLifecycleStateDeleted ): true ,
216+ }
217+ if _ , ok := deletedLifecycleStates [string (response .LifecycleState )]; ! ok {
218+ //resource lifecycle state is not in expected deleted lifecycle states.
219+ return fmt .Errorf ("resource lifecycle state: %s is not in expected deleted lifecycle states" , response .LifecycleState )
220+ }
221+ //resource lifecycle state is in expected deleted lifecycle states. continue with next one.
222+ continue
223+ }
224+
225+ //Verify that exception is for '404 not found'.
226+ if failure , isServiceError := common .IsServiceError (err ); ! isServiceError || failure .GetHTTPStatusCode () != 404 {
227+ return err
228+ }
229+ }
230+ }
231+ if noResourceFound {
232+ return fmt .Errorf ("at least one resource was expected from the state file, but could not be found" )
233+ }
234+
235+ return nil
236+ }
237+
238+ func init () {
239+ if DependencyGraph == nil {
240+ initDependencyGraph ()
241+ }
242+ if ! inSweeperExcludeList ("IdentityTagNamespace" ) {
243+ resource .AddTestSweepers ("IdentityTagNamespace" , & resource.Sweeper {
244+ Name : "IdentityTagNamespace" ,
245+ Dependencies : DependencyGraph ["tagNamespace" ],
246+ F : sweepIdentityTagNamespaceResource ,
247+ })
248+ }
249+ }
250+
251+ func sweepIdentityTagNamespaceResource (compartment string ) error {
252+ // prevent tag deletion when testing, as its a time consuming and sequential operation permitted one per tenancy.
253+ importIfExists , _ := strconv .ParseBool (getEnvSettingWithDefault ("tags_import_if_exists" , "false" ))
254+ if importIfExists {
255+ return nil
256+ }
257+
258+ identityClient := GetTestClients (& schema.ResourceData {}).identityClient
259+ tagNamespaceIds , err := getTagNamespaceIds (compartment )
260+ if err != nil {
261+ return err
262+ }
263+
264+ // clean all tags in namespaces
265+ err = sweepIdentityTagResource (compartment )
266+ if err != nil {
267+ return err
268+ }
269+
270+ for _ , tagNamespaceId := range tagNamespaceIds {
271+ if ok := SweeperDefaultResourceId [tagNamespaceId ]; ! ok {
272+ deleteTagNamespaceRequest := oci_identity.DeleteTagNamespaceRequest {}
273+
274+ deleteTagNamespaceRequest .TagNamespaceId = & tagNamespaceId
275+
276+ deleteTagNamespaceRequest .RequestMetadata .RetryPolicy = getRetryPolicy (true , "identity" )
277+ _ , error := identityClient .DeleteTagNamespace (context .Background (), deleteTagNamespaceRequest )
278+ if error != nil {
279+ fmt .Printf ("Error deleting TagNamespace %s %s, It is possible that the resource is already deleted. Please verify manually \n " , tagNamespaceId , error )
280+ continue
281+ }
282+ waitTillCondition (testAccProvider , & tagNamespaceId , tagNamespaceSweepWaitCondition , time .Duration (3 * time .Minute ),
283+ tagNamespaceSweepResponseFetchOperation , "identity" , true )
284+ }
285+ }
286+ return nil
287+ }
288+
289+ func getTagNamespaceIds (compartment string ) ([]string , error ) {
290+ ids := getResourceIdsToSweep (compartment , "TagNamespaceId" )
291+ if ids != nil {
292+ return ids , nil
293+ }
294+ var resourceIds []string
295+ compartmentId := compartment
296+ identityClient := GetTestClients (& schema.ResourceData {}).identityClient
297+
298+ listTagNamespacesRequest := oci_identity.ListTagNamespacesRequest {}
299+ listTagNamespacesRequest .CompartmentId = & compartmentId
300+ listTagNamespacesRequest .LifecycleState = oci_identity .TagNamespaceLifecycleStateActive
301+ listTagNamespacesResponse , err := identityClient .ListTagNamespaces (context .Background (), listTagNamespacesRequest )
302+
303+ if err != nil {
304+ return resourceIds , fmt .Errorf ("Error getting TagNamespace list for compartment id : %s , %s \n " , compartmentId , err )
305+ }
306+ for _ , tagNamespace := range listTagNamespacesResponse .Items {
307+ id := * tagNamespace .Id
308+ resourceIds = append (resourceIds , id )
309+ addResourceIdToSweeperResourceIdMap (compartmentId , "TagNamespaceId" , id )
310+ }
311+ return resourceIds , nil
312+ }
313+
314+ func tagNamespaceSweepWaitCondition (response common.OCIOperationResponse ) bool {
315+ // Only stop if the resource is available beyond 3 mins. As there could be an issue for the sweeper to delete the resource and manual intervention required.
316+ if tagNamespaceResponse , ok := response .Response .(oci_identity.GetTagNamespaceResponse ); ok {
317+ return tagNamespaceResponse .LifecycleState != oci_identity .TagNamespaceLifecycleStateDeleted
318+ }
319+ return false
320+ }
321+
322+ func tagNamespaceSweepResponseFetchOperation (client * OracleClients , resourceId * string , retryPolicy * common.RetryPolicy ) error {
323+ _ , err := client .identityClient .GetTagNamespace (context .Background (), oci_identity.GetTagNamespaceRequest {
324+ TagNamespaceId : resourceId ,
325+ RequestMetadata : common.RequestMetadata {
326+ RetryPolicy : retryPolicy ,
327+ },
328+ })
329+ return err
330+ }
0 commit comments