def setUp(self): self.Api = Api self.Api._requests_auth = lambda x, y: (x, y) self.account_client = StaxClient("accounts") self.workload_client = StaxClient("workloads") self.assertTrue(self.account_client._initialized) self.assertTrue(self.workload_client._initialized)
def testStaxClient(self): """ Test initializing Stax client """ client = StaxClient("accounts", lambda_client=True) self.assertTrue(client._initialized) self.assertTrue(client._admin)
def testLoadOldSchema(self): """ Test loading Old schema """ self.Config = Config self.Config.load_live_schema = False client = StaxClient("accounts", force=True) self.assertTrue(client._initialized)
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties # update an ACTIVE Stax Direct Connect (DX) Vif by providing the `dx_vif_id` and body parameter networks = StaxClient("networking") body = {"JumboMtu": False, "Tags": {"CostCode": "12345"}} response = networks.UpdateDxVif(dx_vif_id="<dx_vif_uuid>", **body) print(json.dumps(response, indent=4, sort_keys=True))
import os from staxapp.config import Config from staxapp.openapi import StaxClient # The account type to update account_type_id = <Account Type Id> # The new name for the account type account_type_name = <Account Type Name> Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") accounts = StaxClient('accounts') response = accounts.UpdateAccountType( account_type_id = account_type_id, Name = account_type_name ) print(response.json())
import json from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = <Access Key> Config.secret_key = <Secret Key> # The workload to update workload_id = <Workload Id> # The catalogue version id to update the worklaod too catalogue_version_id = <Catalogue Version Id> workload_client = StaxClient('workloads') response = workload_client.UpdateWorkload(workload_id=workload_id, catalogue_version_id=catalogue_version_id) print(json.dumps(response, indent=4, sort_keys=True))
import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("ACCESS_KEY") Config.secret_key = os.getenv("SECRET_KEY") # Read all tasks task_client = StaxClient("tasks") response = task_client.ReadTasks() print(response.json()) # Task Id in the response of a different StaxClient call task_id = <Task Id> # Get a specific task response = task_client.ReadTask(task_id=task_id) print(response.json())
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API Schema for valid body properties networks = StaxClient("networking") # create a DNS Rule within a Stax DNS Resolver by providing the `dns_resolver_uuid` and body properties body = { "DomainName": "test.local", "ForwarderIpAddresses": ["192.168.0.1", "192.168.0.2"], "Name": "on-premises", "Tags": { "CostCode": "12345" } } response = networks.CreateDnsRule(dns_resolver_id="<dns_resolver_uuid>", **body) print(json.dumps(response, indent=4, sort_keys=True))
import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # The name of the token token_name = <Api Token Name> # What access the token will have to the api token_role = <Api Token Role> # Whether the secret to store the token in ssm params # Otherwise it will be returned in the response store_token = <Store Token?> # Create an Api token with access to your Stax Organisation teams = StaxClient("teams") response = teams.CreateApiToken(Name=token_name, Role=token_role, StoreToken=store_token) print(response.json())
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") networks = StaxClient("networking") body = { "Name": "my-dns-rule" } response = networks.UpdateDnsRule(dns_rule_id="<dns_rule_uuid>", **body) print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties networks = StaxClient("networking") # create a DNS Resolver within a Stax Networking Hub by providing the `hub_id` and body properties body = { "Name": "dns resolver", "NumberOfInterfaces": 2, "Tags": { "CostCode": "12345" } } response = networks.CreateDnsResolver(hub_id="<hub_uuid>", **body) print(json.dumps(response, indent=4, sort_keys=True))
import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # Read all accounts within your Stax Organisation accounts = StaxClient("accounts") response = accounts.ReadAccounts() print(response.json()) # Read all active accounts within your Stax Organisation and include tags in the response accounts = StaxClient("accounts") response = accounts.ReadAccounts(filter="ACTIVE", include_tags=True) print(response.json())
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties networks = StaxClient("networking") # example 1: create a Stax Hub Prefix List body = { "Entries": ["192.168.0.1/32", "192.168.0.2/32"], "MaxEntries": 3, "Name": "example", "RouteTableTypes": ["INFRASTRUCTURE", "ISOLATED"], "Tags": { "CostCode": "12345" }, "TargetId": "ec5eaa8f-da06-4935-b5ce-05bd957c8bdc", "TargetType": "VPC", "Zones": ["ZONE1", "ZONE2"] } response = networks.CreateHubPrefixList(hub_id="<hub_uuid>", **body) print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties networks = StaxClient("networking") # example 1: create a Stax Direct Connect (DX) association between a Networking Hub and a DX Gateway # providing a Networking Hub Id will attempt to associate the DX Gateway that contains Transit VIFs to the Networking Hub's Transit Gateway body = { "NetworkingHubId": "<hub_uuid>", "Prefixes": ["192.168.0.0/24", "192.168.1.0/24"] } response = networks.CreateDxAssociation(dx_gateway_id="<dx_gateway_uuid>", **body) print(json.dumps(response, indent=4, sort_keys=True)) # example 2: create a Stax DX association between a Stax Networking Hub's VPC and a Stax DX Gateway # providing a VPC Id will attempt to associate the DX Gateway that contains private VIFs to the VPCs virtual private gateway body = { "VpcId": "<vpc_uuid>",
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") accounts = StaxClient('accounts') # An array of Account Type-Policy dicts to enable or an empty array # Properties: AccountTypeId, PolicyId policies_to_add = <An array of Account type-Policies?> # An array of Account Type-Policy dicts to disable or an empty arry policies_to_remove = <An array of Account type-Policies?> response = accounts.UpdatePolicies( AddPolicies= policies_to_add, RemovePolicies= policies_to_remove ) print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties # only Stax Direct Connect (DX) Associations with a status of ACTIVE, CREATE_FAILED and DELETE_FAILED can be deleted networks = StaxClient("networking") # delete a Stax DX Association by providing the `dx_association_uuid` response = networks.DeleteDxAssociation( dx_association_id="<dx_association_uuid>") print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # Read all workload catalogue items within your Stax Organisation workloads = StaxClient("workloads") response = workloads.ReadCatalogueItems() print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties # only Stax Direct Connect (DX) Gateway with a status of ACTIVE, CREATE_FAILED and DELETE_FAILED can be deleted # Stax DX Gateways with an active DX Connection cannot be deleted networks = StaxClient("networking") # delete a Stax DX Gateway by providing the `dx_gateway_id` response = networks.DeleteDxGateway(dx_gateway_id="<dx_gateway_uuid>") print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties # if `status` filter is not provided, only resources with the following statuses will be returned by default: ACTIVE,CREATE_IN_PROGRESS,CREATE_FAILED,UPDATE_IN_PROGRESS networks = StaxClient("networking") # read all Stax VPN Customer Gateways in the Organization response = networks.ReadVpnCustomerGateways() # read all Stax VPN Customer Gateways in the Organization, filtered by status response = networks.ReadVpnCustomerGateways( status= "ACTIVE,CREATE_IN_PROGRESS,CREATE_FAILED,DELETE_IN_PROGRESS,DELETED,DELETE_FAILED,UPDATE_IN_PROGRESS" ) # read the details of a single Stax VPN Customer Gateway by providing the `vpn_customer_gateway_id` response = networks.ReadVpnCustomerGateways( vpn_customer_gateway_id="<vpn_customer_gateway_uuid>") # read all Stax VPN Customer Gateways associated with a Networking Hub by providing the `hub_id` response = networks.ReadVpnCustomerGateways(hub_id="<hub_uuid>") print(json.dumps(response, indent=4, sort_keys=True))
import os import json from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") networks = StaxClient("networking") body = {"Name": "my-vpc-tester"} response = networks.UpdateVpc(vpc_id="<vpc_id>", **body) print(response.json())
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # Id of the group to update group_id = <Group Id> # Name to rename the group group_name = <Group Name> # Create a Stax groups teams = StaxClient("teams") response = teams.UpdateGroup( group_id=group_id, Name=group_name ) print(json.dumps(response, indent=4, sort_keys=True))
# - Logged into the deployment bucket account # - Logged into the SDK Config.access_key = <Access Key> Config.secret_key = <Secret Key> # Deployment bucket name deployment_bucket_name = <Deployment bucket workload name> # Path to Cloudformation file to deploy as a workload cloudformation_manifest_path = <Cloudformation File> # Catalogue Id catalogue_id = <Catalogue Id> #The new Catalogue version catalogue_version = <Catalogue Version> workload_client = StaxClient('workloads') # Get the AWS name of the deployment bucket ssm = boto3.client("ssm") bucket_name = ssm.get_parameter(Name=f"/workloads/{deployment_bucket_name}/BucketName", WithDecryption=True) #Upload the cfn to the deployment bucket s3 = boto3.resource('s3') cfn_name = f'{catalogue_version}-{catalogue_name}.yaml' s3.Bucket(bucket_name["Parameter"]["Value"]).upload_file(cloudformation_manifest_path, cfn_name) # # Invoke the Stax SDK workload_client = StaxClient("workloads") # Make the cfn into a workload catalogue item manifest_body = f"""Resources:
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties # if `status` filter is not provided, only resources with the following statuses will be returned by default: ACTIVE,CREATE_IN_PROGRESS,CREATE_FAILED,UPDATE_IN_PROGRESS networks = StaxClient("networking") # read all CIDR Ranges in the Organization response = networks.ReadCidrRanges() # read all CIDR Ranges in the Organization, filtered by status response = networks.ReadCidrRanges( status= "ACTIVE,CREATE_IN_PROGRESS,CREATE_FAILED,DELETE_IN_PROGRESS,DELETED,DELETE_FAILED,UPDATE_IN_PROGRESS" ) # read the details of a CIDR Range by providing the `range_id` response = networks.ReadCidrRanges(range_id="<range_uuid>") print(json.dumps(response, indent=4, sort_keys=True))
import json from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = <Access key> Config.secret_key = <Secret key> # Catalogue Id catalogue_id = <Catalogue Id> #Version Id version_id = <Catalogue Version> workload_client = StaxClient("workloads") response = workload_client.ReadCatalogueVersion(catalogue_id=catalogue_id, version_id=version_id) print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") networks = StaxClient("networking") response = networks.DeleteDnsResolver(dns_resolver_id="<resolver_uuid>") print(json.dumps(response, indent=4, sort_keys=True))
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # refer to the Stax API schema for valid body properties # update an ACTIVE Stax VPN Customer Gateway by providing the `vpn_customer_gateway_id` and body parameter networks = StaxClient("networking") body = {"Name": "vpn-gw123", "Tags": {"CostCode": "12345"}} response = networks.UpdateVpnCustomerGateway( vpn_customer_gateway_id="<<vpn_customer_gateway_uuid>>", **body) print(json.dumps(response, indent=4, sort_keys=True))
# Deployment bucket name deployment_bucket_name = sys.argv[1] Config.access_key = os.getenv('STAX_ACCESS_KEY') Config.secret_key = os.getenv('STAX_SECRET_KEY') # Path to Cloudformation file to create a catalogue of cloudformation_manifest_path = f"{os.path.dirname(__file__)[:-4]}/cloudformation/api_token_cfn.yaml" # Catalogue Name catalogue_name = 'stax-api-token-access' # Catalogue version catalogue_version = '1.0.0' cfn_name = f'{catalogue_version}-{catalogue_name}.yaml' workload_client = StaxClient("workloads") try: # Check if the workload already exists catalogue = workload_client.ReadCatalogueItems(name=catalogue_name, filter="ACTIVE") print(f"{catalogue['WorkloadCatalogues'][0]['WorkloadCatalogueItems'][0]['Name']} already exists ") except ApiException as e: if e.status_code == 404: # Get the AWS name of the deployment bucket sts = boto3.client("sts") ssm = boto3.client("ssm") bucket_name = ssm.get_parameter(Name=f"/workloads/{deployment_bucket_name}/BucketName", WithDecryption=True) #Upload the cfn to the deployment bucket s3 = boto3.resource('s3')
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") policy_id = <Policy Id> # Delete a policy organisations = StaxClient("organisations") response = organisations.DeletePolicy( policy_id = policy_id, ) print(json.dumps(response, indent=4, sort_keys=True))
import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") # Read all users within your Stax Organisation teams = StaxClient("teams") response = teams.ReadUsers() print(response.json())
import json import os from staxapp.config import Config from staxapp.openapi import StaxClient Config.access_key = os.getenv("STAX_ACCESS_KEY") Config.secret_key = os.getenv("STAX_SECRET_KEY") networks = StaxClient("networking") body = {"Name": "my-own-range"} response = networks.UpdateCidrRange(range_id="<range_uuid>", **body) print(json.dumps(response, indent=4, sort_keys=True))