-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
279 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#!/bin/bash | ||
# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. | ||
# This script provides an example of how to use compartments in the CLI in terms of: | ||
# | ||
# - Managing compartments by performing create, read (get/list) operations on them. | ||
# | ||
# WARNING: Compartments currently does not supporting the hard delete. Once you created the compartments with the script, you cannot hard delete them. | ||
# WARNING: Compartments supported operations can be found https://docs.cloud.oracle.com/iaas/api/#/en/identity/20160918/Compartment/. | ||
# | ||
# Requirements for running this script: | ||
# - OCI CLI v2.4.35 or later (you can check this by running oci --version). | ||
# - jq (https://stedolan.github.io/jq/) for JSON querying and manipulation of CLI output. This may be a useful utility in general | ||
# and may help cater to scenarios which can't be wholly addressed by the --query option in the CLI. | ||
|
||
TENANCY_ID="" # Your tenancy ID | ||
|
||
# script to exit on first error | ||
set -e | ||
|
||
########################################################## | ||
# Setup the compartments like the following tree | ||
# Here is the compartment Tree in this Test | ||
# Tenancy | ||
# | | ||
# --- CP-1 | ||
# | | ||
# | | ||
# --- CP-2 | ||
# | | | ||
# | --- CP-21 | ||
# | | | ||
# | --- CP-211 | ||
# | | ||
# --- CP-3 | ||
# | | ||
# --- CP-31 | ||
########################################################## | ||
|
||
echo "WARNING: Compartments currently does not supporting the hard delete.Once you created the compartments with the script, you cannot hard delete them" | ||
echo "WARNING: Compartments supported operations can be found https://docs.cloud.oracle.com/iaas/api/#/en/identity/20160918/Compartment/" | ||
|
||
# Create first level of compartments (CP1, CP2, CP3) | ||
echo "Creating Compartment CP1" | ||
CREATED_COMPARTMENT=$(oci iam compartment create --compartment-id $TENANCY_ID --name CP-1 --description "CP1") | ||
COMPARTMENT_CP1_ID=$(jq -r '.data.id' <<< "$CREATED_COMPARTMENT") | ||
echo "Compartment-CP1 OCID: ${COMPARTMENT_CP1_ID}" | ||
|
||
echo "Creating Compartment CP2" | ||
CREATED_COMPARTMENT=$(oci iam compartment create --compartment-id $TENANCY_ID --name CP-2 --description "CP2") | ||
COMPARTMENT_CP2_ID=$(jq -r '.data.id' <<< "$CREATED_COMPARTMENT") | ||
echo "Compartment-CP2 OCID: ${COMPARTMENT_CP2_ID}" | ||
|
||
echo "Creating Compartment CP3" | ||
CREATED_COMPARTMENT=$(oci iam compartment create --compartment-id $TENANCY_ID --name CP-3 --description "CP3") | ||
COMPARTMENT_CP3_ID=$(jq -r '.data.id' <<< "$CREATED_COMPARTMENT") | ||
echo "Compartment-CP3 OCID: ${COMPARTMENT_CP3_ID}" | ||
|
||
# List first level compartments under tenancy | ||
echo "List Compartments under Tenancy" | ||
LIST_COMPARTMENTS=$(oci iam compartment list --compartment-id $TENANCY_ID) | ||
|
||
echo "List Compartments under Tenancy with accessibleLevel == accessible" | ||
LIST_COMPARTMENTS=$(oci iam compartment list --compartment-id $TENANCY_ID --access-level accessible) | ||
|
||
|
||
# If we create/update and then try to use compartments straight away, sometimes we can get a 404. To try and avoid this, the script | ||
# adds a short delay between the compartment management operations. | ||
# Also sleep is not needed but kept as a safety measure for worst case for data plane sync with control plan changes. | ||
sleep 10 | ||
|
||
# Create second level of compartments (CP21, CP31) | ||
echo "Creating Compartment CP21 under CP2" | ||
CREATED_COMPARTMENT=$(oci iam compartment create --compartment-id $COMPARTMENT_CP2_ID --name CP-21 --description "CP21") | ||
COMPARTMENT_CP21_ID=$(jq -r '.data.id' <<< "$CREATED_COMPARTMENT") | ||
echo "Compartment-CP21 OCID: ${COMPARTMENT_CP21_ID}" | ||
|
||
echo "Creating Compartment CP31 under CP3" | ||
CREATED_COMPARTMENT=$(oci iam compartment create --compartment-id $COMPARTMENT_CP3_ID --name CP-31 --description "CP31") | ||
COMPARTMENT_CP31_ID=$(jq -r '.data.id' <<< "$CREATED_COMPARTMENT") | ||
echo "Compartment-CP31 OCID: ${COMPARTMENT_CP31_ID}" | ||
|
||
|
||
# If we create/update and then try to use compartments straight away, sometimes we can get a 404. To try and avoid this, the script | ||
# adds a short delay between the compartment management operations. | ||
# Also sleep is not needed but kept as a safety measure for worst case for data plane sync with control plan changes. | ||
sleep 10 | ||
|
||
# Create third level of compartments (CP211) | ||
echo "Creating Compartment CP211 under CP21" | ||
CREATED_COMPARTMENT=$(oci iam compartment create --compartment-id $COMPARTMENT_CP21_ID --name CP-211 --description "CP211") | ||
COMPARTMENT_CP211_ID=$(jq -r '.data.id' <<< "$CREATED_COMPARTMENT") | ||
echo "Compartment-CP21 OCID: ${COMPARTMENT_CP211_ID}" | ||
|
||
|
||
# List all level compartments under tenancy | ||
echo "List Compartments under Tenancy with compartment-id-in-subtree == true" | ||
LIST_COMPARTMENTS=$(oci iam compartment list --compartment-id $TENANCY_ID --compartment-id-in-subtree true) | ||
|
||
# List all level compartments under tenancy with accessLevel == Accessible | ||
echo "List Compartments under Tenancy with compartment-id-in-subtree == true and accessLevel == accessible" | ||
LIST_COMPARTMENTS=$(oci iam compartment list --compartment-id $TENANCY_ID --access-level accessible --compartment-id-in-subtree true) | ||
|
||
# List first level compartments under CP2 | ||
echo "List Compartments under CP2" | ||
LIST_COMPARTMENTS=$(oci iam compartment list --compartment-id $COMPARTMENT_CP2_ID) | ||
|
||
# List first level compartments under CP21 | ||
echo "List Compartments under CP21" | ||
LIST_COMPARTMENTS=$(oci iam compartment list --compartment-id $COMPARTMENT_CP21_ID) | ||
|
||
# List first level compartments under CP3 | ||
echo "List Compartments under CP3" | ||
LIST_COMPARTMENTS=$(oci iam compartment list --compartment-id $COMPARTMENT_CP3_ID) | ||
|
||
echo "DONE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/bin/bash | ||
# This script provides a basic example of how to use the Nat Gateway service in the CLI. | ||
# The two variables at the beginning of the script must be specified accordingly: | ||
# | ||
# * COMPARTMENT_ID: The OCID of the compartment where we'll create our file system and related resources | ||
# | ||
# The script will demonstrate: | ||
# | ||
# * Creating a new nat gateway | ||
# * Getting a nat gateway | ||
# * Updating a nat gateway | ||
# * Deleting a nat gateway | ||
# | ||
# | ||
# Requirements for running this script: | ||
# - OCI CLI v2.4.34 or later (you can check this by running oci --version) | ||
# - jq (https://stedolan.github.io/jq/) for JSON querying of CLI output. This may be a useful utility in general and may help cater to scenarios | ||
# which can't be wholly addressed by the --query option in the CLI | ||
|
||
set -e | ||
|
||
COMPARTMENT_ID="" | ||
|
||
# First we will create a VCN and a subnet. Since these resources have a lifecycle state, we can create them and use | ||
# the --wait-for-state option so that our command will only return/complete when the resouce enters the desired | ||
# state (in this case AVAILABLE) | ||
VCN_ID=$(oci network vcn create -c $COMPARTMENT_ID --display-name createNatgwExampleVcn --cidr-block 10.0.0.0/16 --wait-for-state AVAILABLE --query 'data.id' --raw-output 2>/dev/null) | ||
echo "VCN OCID: ${VCN_ID}" | ||
|
||
echo | ||
# First we create a nat gateway. A nat gateway has a lifecycle state so we can use the --wait-for-state | ||
# option so that our command will only return/complete when the nat gateway reaches the desired state. | ||
NAT_GATEWAY_ID=$(oci network nat-gateway create -c $COMPARTMENT_ID --vcn-id $VCN_ID --display-name exampleNatGateway --wait-for-state AVAILABLE --query data.id --raw-output) | ||
echo "Nat Gateway OCID: $NAT_GATEWAY_ID" | ||
echo "" | ||
|
||
# Update routing for the subnet by creating a route table with a route rule that directs internet-bound traffic to the Nat Gateway | ||
# Create route table and wait for it to become available | ||
ROUTE_RULE='[{"cidrBlock":"0.0.0.0/0","networkEntityId":"'${NAT_GATEWAY_ID}'"}]' | ||
echo "Create route table and add Nat Gateway rule" | ||
echo "=========================" | ||
ROUTE_TABLE_ID=$(oci network route-table create -c $COMPARTMENT_ID --route-rules $ROUTE_RULE --vcn-id $VCN_ID --wait-for-state AVAILABLE --query data.id --raw-output) | ||
echo "Route Table OCID: $ROUTE_TABLE_ID" | ||
echo "" | ||
|
||
# We can show the route table directed to the nat gateway | ||
echo "Get route table" | ||
echo "=========================" | ||
oci network route-table get --rt-id $ROUTE_TABLE_ID | ||
echo "" | ||
|
||
# We can list all nat gateways in a compartment. This is a paginated call and we can use the --all option to get | ||
# all results rather than having to manually deal with page tokens | ||
echo "Listing all nat gateways" | ||
echo "=========================" | ||
oci network nat-gateway list -c $COMPARTMENT_ID --all | ||
echo "" | ||
|
||
# We can get a specific nat gateway | ||
echo "Get nat gateway" | ||
echo "=========================" | ||
oci network nat-gateway get --nat-gateway-id $NAT_GATEWAY_ID | ||
echo "" | ||
|
||
# We can update a nat gateway to block traffic through it | ||
echo "Update nat gateway" | ||
echo "=========================" | ||
oci network nat-gateway update --nat-gateway-id $NAT_GATEWAY_ID --block-traffic true | ||
echo "" | ||
|
||
# Now clean up resources. Since these resources have lifecycle states, we can use --wait-for-state so that the command | ||
# In order to delete nat gateway, There must not be a route table that lists the NAT gateway as a target. | ||
# only completes/returns when the resource has entered the DELETED (or equivalent) state | ||
oci network route-table delete --rt-id $ROUTE_TABLE_ID --force --wait-for-state TERMINATED | ||
echo "Deleted Route Table" | ||
|
||
oci network nat-gateway delete --nat-gateway-id $NAT_GATEWAY_ID --force --wait-for-state TERMINATED | ||
echo "Deleted Nat Gateway" | ||
|
||
oci network vcn delete --vcn-id $VCN_ID --force --wait-for-state TERMINATED | ||
echo "Deleted VCN" | ||
echo "" | ||
|
||
echo "Script Finished" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.