forked from oracle/oci-powershell-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucketTagging_ObjectStorage.ps1
More file actions
77 lines (63 loc) · 3.74 KB
/
Copy pathBucketTagging_ObjectStorage.ps1
File metadata and controls
77 lines (63 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<#
This example demonstrates tagging Buckets Resource in Object Storage Service.
This example requires:
1) Module OCI.PSModules.Objectstorage. Install the module from Powershell Gallery.
2) Setting up the environment variable CompartmentId to a Compartment OCID.
3) Setting up the environment variables TagNS,TagKey to an existing tag namespace and key in your tenancy as explained in https://docs.cloud.oracle.com/Content/General/Concepts/resourcetags.htm
#>
$UserErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrEmpty($env:CompartmentId) -or [string]::IsNullOrEmpty($env:TagNS) -or [string]::IsNullOrEmpty($env:TagKey)) {
Throw 'Configure $env:CompartmentId,$env:TagNS and $env:TagKey in the PS Session'
}
try {
#Import the module
Import-Module OCI.PSModules.Objectstorage
#Setup
$RandomSufix = Get-Random -Minimum 1 -Maximum 1000
$DisplayName = "PSExample" + $RandomSufix
$BucketName = $DisplayName -join "Bucket"
#Read CompartmentId environment variable
$CompartmentId = $env:CompartmentId
$TagNS = $env:TagNS
$TagKey = $env:TagKey
#Get the namespace of the account
Write-Host "Get-OCIObjectStorageNamespace -CompartmentId $CompartmentId"
$NamespaceName = Get-OCIObjectStorageNamespace -CompartmentId $CompartmentId
$NamespaceName
$FreeformTags = New-Object 'System.Collections.Generic.Dictionary[String,String]'
$FreeformTags.Add("FFTagKey", "FFTagValue")
$Tags = New-Object 'System.Collections.Generic.Dictionary[String,Object]'
$Tags.Add($TagKey, "DefinedValue")
$DefinedTags = New-Object 'System.Collections.Generic.Dictionary[String,System.Collections.Generic.Dictionary[String,Object]]'
$DefinedTags.Add($TagNS, $Tags)
#Create bucket details
$BucketDetails = New-Object Oci.ObjectstorageService.Models.CreateBucketDetails
$BucketDetails.CompartmentId = $CompartmentId
$BucketDetails.Name = $BucketName
$BucketDetails.FreeformTags = $FreeformTags
$BucketDetails.DefinedTags = $DefinedTags
#Create a new object store bucket
Write-Host "New-OCIObjectStorageBucket -NamespaceName $NamespaceName -CreateBucketDetails $BucketDetails"
$Bucket = New-OCIObjectStorageBucket -NamespaceName $NamespaceName -CreateBucketDetails $BucketDetails
#Read the last response from history
$OCICmdletHistory.LastResponse.Bucket
Write-Host "Get-OCIObjectStorageBucket -NamespaceName $NamespaceName -BucketName $Bucket.Name"
Get-OCIObjectStorageBucket -NamespaceName $NamespaceName -BucketName $Bucket.Name | Out-Host
Write-Host "List Buckets without tags: Get-OCIObjectStorageBucket -NamespaceName $NamespaceName -BucketName $Bucket.Name"
Get-OCIObjectStorageBucketsList -NamespaceName $NamespaceName -CompartmentId $CompartmentId -All | Out-Host
$TagsList = New-Object Collections.Generic.List[Oci.ObjectstorageService.Requests.ListBucketsRequest+FieldsEnum]
$TagsList.Add([Oci.ObjectstorageService.Requests.ListBucketsRequest+FieldsEnum]::Tags)
Write-Host "List Buckets with tags:Get-OCIObjectStorageBucketsList -NamespaceName $NamespaceName -CompartmentId $CompartmentId -Fields $TagsList"
Get-OCIObjectStorageBucketsList -NamespaceName $NamespaceName -CompartmentId $CompartmentId -Fields $TagsList | Out-Host
}
finally {
#To Maximize possible clean ups, continue on error
$ErrorActionPreference = "Continue"
#Remove the created bucket asking explicit user confirmation
if ($null -ne $Bucket) {
Write-Host "Remove-OCIObjectStorageBucket -NamespaceName $NamespaceName -BucketName $BucketName -FullResponse"
Remove-OCIObjectStorageBucket -NamespaceName $NamespaceName -BucketName $BucketName -FullResponse
}
$ErrorActionPreference = $UserErrorActionPreference
}