/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ /**
  *
  *  This script provides an example on how to use tagging in the SDK for TypeScript to manage tags and tag namespaces,
  as well as how to apply freeform and defined tags to a resource.
 */ import * as identity from "oci-identity" ; import core = require ( "oci-core" ) ; import common = require ( "oci-common" ) ; const provider = new common . ConfigFileAuthenticationDetailsProvider ( ) ; ( async ( ) => { const compartmentId = provider . getTenantId ( ) ; const identityClient = new identity . IdentityClient ( { authenticationDetailsProvider : provider } ) ; const namespace = { compartmentId : compartmentId , name : `typeScriptExample_namespace${ Math . floor ( Math . random ( ) * Math . floor ( 1000000 ) ) }` , description : "SDK for TypeScript example tag namespace" } ; const namespaceReq = { createTagNamespaceDetails : namespace } ; const createTagNamespace = await identityClient . createTagNamespace ( namespaceReq ) ; const tagNamespaceId = createTagNamespace . tagNamespace . id ; console . log ( "Created tag namespace: " , createTagNamespace . tagNamespace . id ) ; const tagOneName = { name : "typeScriptExample_tagOne" , description : "SDK for TypeScript example tag one" } ; const createTagOneReq = { tagNamespaceId : tagNamespaceId , createTagDetails : tagOneName } ; const createTagOneRes = await identityClient . createTag ( createTagOneReq ) ; console . log ( "Created Tag: " , createTagOneRes . tag ) ; const tagTwoName = { name : "typeScriptExample_tagTwo" , description : "SDK for TypeScript example tag two" } ; const createTagTwoReq = { tagNamespaceId : tagNamespaceId , createTagDetails : tagTwoName } ; const createTagTwoRes = await identityClient . createTag ( createTagTwoReq ) ; console . log ( "Created Tag: " , createTagTwoRes . tag ) ; const updateTagOneReq = { tagNamespaceId : tagNamespaceId , tagName : tagOneName . name , updateTagDetails : { isRetired : true } } ; let updateTagOneRes = await identityClient . updateTag ( updateTagOneReq ) ; console . log ( "Updated tag (retired): " , updateTagOneRes . tag ) ; const getTagNamespaceReq = { tagNamespaceId : tagNamespaceId } ; const getTagOneReq = { tagNamespaceId : tagNamespaceId , tagName : tagOneName . name } ; const getTagTwoReq = { tagNamespaceId : tagNamespaceId , tagName : tagTwoName . name } ; const tagNamespace = ( await identityClient . getTagNamespace ( getTagNamespaceReq ) ) . tagNamespace ; const tagOne = ( await identityClient . getTag ( getTagOneReq ) ) . tag ; const tagTwo = ( await identityClient . getTag ( getTagTwoReq ) ) . tag ; const listTagNamespacesReq = { compartmentId : compartmentId } ; const listTagsReq = { tagNamespaceId : tagNamespaceId } ; const tagNamespaces = ( await identityClient . listTagNamespaces ( listTagNamespacesReq ) ) . items ; const tagsInNamespaces = await ( await identityClient . listTags ( listTagsReq ) ) . items ; updateTagOneReq [ "updateTagDetails" ] = { "isRetired" : false } ; updateTagOneRes = await identityClient . updateTag ( updateTagOneReq ) ; console . log ( "Updated tag (reactivated): " , updateTagOneRes . tag ) ; const virtualNetwork = new core . VirtualNetworkClient ( { authenticationDetailsProvider : provider } ) ; let numOfTries = 0 ; let vcnId ; while ( true ) { try { const createVCNDetails = { cidrBlock : "10.0.0.0/16" , compartmentId : compartmentId , displayName : "SDK for TypeScript tagging example VCN" , dnsLabel : `vcn${ Math . floor ( Math . random ( ) * Math . floor ( 1000000 ) ) }` , freeformTags : { "free" : "form" , "another" : "item" } , definedTags : { [ tagNamespace . name ] : { [ tagOneName . name ] : "hello" , [ tagTwoName . name ] : "world " } } } ; const createVCNRequest = { createVcnDetails : createVCNDetails } ; const createVCNResponse = virtualNetwork . createVcn ( createVCNRequest ) ; vcnId = ( await createVCNResponse ) . vcn . id ; console . log ( "Created VCN with tags: " , createVCNResponse ) ; break ; } catch ( err ) { if ( err . statusCode === 400 ) { console . log ( "Retrying on 400: " , err ) ; numOfTries += 1 ; if ( numOfTries >= 3 ) { throw "Reached max number of retries, something else is going on." ; } else { await new Promise ( r => setTimeout ( r , 5000 ) ) ; } } else { throw "Error" ; } } } let updateVcnDetails = { freeformTags : { total : "replace" } , definedTags : { [ tagNamespace . name ] : { [ tagTwoName . name ] : "also replaced" } } } ; let updateVCNReq = { vcnId : vcnId , updateVcnDetails : updateVcnDetails } ; let updateVCNRes = await virtualNetwork . updateVcn ( updateVCNReq ) ; console . log ( "Updated tags on VCN: " , updateVCNRes . vcn ) ; updateVcnDetails = { freeformTags : { } , definedTags : { } } ; updateVCNReq = { vcnId : vcnId , updateVcnDetails : updateVcnDetails } ; updateVCNRes = await virtualNetwork . updateVcn ( updateVCNReq ) ; console . log ( "Removed tags on VCN: " , updateVCNRes . vcn ) ; let updateTagNamespaceDetails = { isRetired : true } ; let updateTagNameSpaceReq = { tagNamespaceId : tagNamespaceId , updateTagNamespaceDetails : updateTagNamespaceDetails } ; let updateTagNamespaceRes = await identityClient . updateTagNamespace ( updateTagNameSpaceReq ) ; console . log ( "Updated tag namespace (retired): " , updateTagNamespaceRes . tagNamespace ) ; console . log ( "Tags in namespace: " , ( await identityClient . listTags ( listTagsReq ) ) . items ) ; updateTagNamespaceDetails = { isRetired : false } ; updateTagNameSpaceReq = { tagNamespaceId : tagNamespaceId , updateTagNamespaceDetails : updateTagNamespaceDetails } ; updateTagNamespaceRes = await identityClient . updateTagNamespace ( updateTagNameSpaceReq ) ; console . log ( "Updated tag namespace (reactivated): " , updateTagNamespaceRes . tagNamespace ) ; console . log ( "Tags in namespace: " , ( await identityClient . listTags ( listTagsReq ) ) . items ) ; updateTagNamespaceDetails = { isRetired : false } ; updateTagNameSpaceReq = { tagNamespaceId : tagNamespaceId , updateTagNamespaceDetails : updateTagNamespaceDetails } ; identityClient . updateTagNamespace ( updateTagNameSpaceReq ) ; } ) ( ) ;