how to attach AWS scp-organization unit's (OUs) policies to particular organization unit(OU) using python script? - python-3.x

YAML file:
Organization-unit:
xyz
Organization-unit:
xyz2
Policies:
- name: scp-xyz
description:
- name: scp-xyz2
description:
using python how to attach xyz organization unit to scp-xyz only under aws organization.
output: list of policies and organization unit info
{'Policies': [{'Id': 'p-xw7j86as', 'Arn': '', 'Name': 'scp-xyz', 'Description': 'Allows access to every operation', 'Type': 'SERVICE_CONTROL_POLICY', 'AwsManaged': True}
{'Policies': [{'Id': 'p-xw7j006as', 'Arn': '', 'Name': 'scp-xyz2', 'Description': 'Allows access to every operation', 'Type': 'SERVICE_CONTROL_POLICY', 'AwsManaged': True}
{'OrganizationalUnit': {'Id': 'ou-uwjh-87', 'Arn': 'a', 'Name': 'xyz'}
{'OrganizationalUnit': {'Id': 'ou-uw-87', 'Arn': 'a', 'Name': 'xyz2'}
I have below sample to attach policy to OU:
response = client.attach_policy(
PolicyId='string', #policy ID string requires "p-" followed by from 8 to
128 lower-case letters or digits.
TargetId='string' # ID of OU
)
Can anyone please guide me how can I do this task ?
REsult should be like below :
[![SCP-xyz policy should attach with xyz organization unit][1]][1]

Related

Create cloudwatch alarm with multiple metrics and math expression (python3)

Here is an example of how to create cloudwatch alarm using too many metrics and Math expression using boto3.
asg_name : autoscaling group name
import boto3
cloudwatch = boto3.client('cloudwatch', region_name="eu-west-1")
def create_cloudwatch_alarm(asg_name):
cloudwatch.put_metric_alarm(
ActionsEnabled=True,
AlarmName=asg_name,
ComparisonOperator='GreaterThanThreshold',
DatapointsToAlarm=3,
EvaluationPeriods=3,
Metrics=[{'Expression': 'IF(m1 > m2, 1, 0)',
'Id': 'e2',
'Label': 'Compare Running vs desired capacity',
'ReturnData': True},
{'Id': 'm1',
'MetricStat': {'Metric': {'Dimensions': [{'Name': 'AutoScalingGroupName',
'Value': asg_name}],
'MetricName': 'GroupDesiredCapacity',
'Namespace': 'AWS/AutoScaling'},
'Period': 300,
'Stat': 'Average'},
'ReturnData': False},
{'Id': 'm2',
'MetricStat': {'Metric': {'Dimensions': [{'Name': 'AutoScalingGroupName',
'Value': asg_name}],
'MetricName': 'GroupInServiceInstances',
'Namespace': 'AWS/AutoScaling'},
'Period': 300,
'Stat': 'Average'},
'ReturnData': False}],
Threshold=0.1,
TreatMissingData='missing'
)
According AWS's documentation, you can only create alarm on up to 10 metrics with metrics math : https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create-alarm-on-metric-math-expression.html

Pulling item properties from Microsoft Sharepoint document library with Microsoft Graph API

I'm able to successfully pull file metadata from my SharePoint library with the Microsoft Graph API, but am having trouble pulling the properties of an item:
I can get a partial list of properties using this endpoint:
https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{}/items/{}/children?$expand=listItem($expand=fields)
But the list that comes from this endpoint doesn't match the list of properties that exists on the item.
For example, below is a list of fields that come from that endpoint - you can see that '.Push Too Salsify.' (one of the fields I need) is not present. There are also other fields that exist but don't appear in the item properties:
{'ParentLeafNameLookupId': '466', 'CLIPPING_x0020_STATUS': 'Not Started', 'Edit': '0', 'EditorLookupId': '67', '_ComplianceTagWrittenTime': '', 'RequiredField': 'teams/WORKFLOWDEMO/Shared Documents/1062CQP6.Phase4/1062CQP-Phase4-Size.tif', 'PM_x0020_SIGN_x0020_OFF': 'No', 'QA_x0020_APPROVED': 'No', 'ImageWidth': 3648, 'PM_x0020_Approval_x0020_Status': '-', 'AuthorLookupId': '6', 'SelectedFlag': '0', 'NameOrTitle': '1062CQP-Phase4-Size.tif', 'ItemChildCount': '0', 'FolderChildCount': '0', 'LinkFilename': '1062CQP-Phase4-Size.tif', 'ParentVersionStringLookupId': '466', 'PHOTOSTATUS': 'Not Started', '#odata.etag': '"c4b7516e-64df-46d2-b916-a1ee6f29d24a,8"', 'Thumbnail': '3648', '_x002e_Approval_x0020_Status_x002e_': 'Approved', 'Date_x0020_Created': '2019-10-09T04:25:40Z', '_CommentCount': '', 'Created': '2019-10-09T04:25:33Z', 'PreviewOnForm': '0', '_ComplianceTag': '', 'FileLeafRef': '1062CQP-Phase4-Size.tif', 'ImageHeight': 3648, 'LinkFilenameNoMenu': '1062CQP-Phase4-Size.tif', '_ComplianceFlags': '', 'ContentType': 'Document', 'Preview': '3648', 'ImageSize': '3648', 'Product_x0020_Category': 'Baseball', 'DATE_x0020_ASSIGNED': '2019-10-09T04:25:40Z', 'DateCreated': '2019-10-09T04:25:40Z', 'WORKFLOW_x0020_SELECTION': ['Select'], 'Predecessors': [], 'FileType': 'tif', 'LEGAL_x0020_APPROVED': 'No', 'PUSH_x0020_READY': False, 'FileSizeDisplay': '74966432', 'id': '466', '_LikeCount': '', '_ComplianceTagUserId': '', 'Modified': '2019-10-09T14:41:25Z', 'DocIcon': 'tif', '_UIVersionString': '0.7', '_CheckinComment': ''}
Any help would be greatly appreciated. I've scoured the documentation and can't seem to find the correct endpoint to pull item properties from a Sharepoint DriveItem.

Python nested json to csv

I can't convert this Json to csv. I have been trying with different solutions posted here using panda or other parser but non solved this.
This is a small extract of the big json
{'data': {'items': [{'category': 'cat',
'coupon_code': 'cupon 1',
'coupon_name': '$829.99/€705.79 ',
'coupon_url': 'link3',
'end_time': '2017-12-31 00:00:00',
'language': 'sp',
'start_time': '2017-12-19 00:00:00'},
{'category': 'LED ',
'coupon_code': 'code',
'coupon_name': 'text',
'coupon_url': 'link',
'end_time': '2018-01-31 00:00:00',
'language': 'sp',
'start_time': '2017-10-07 00:00:00'}],
'total_pages': 1,
'total_results': 137},
'error_no': 0,
'msg': '',
'request': 'GET api/ #2017-12-26 04:50:02'}
I'd like to get an output like this with the columns:
category, coupon_code, coupon_name, coupon_url, end_time, language, start_time
I'm running python 3.6 with no restrictions.

Getting tags from python 3 dictionary object. AWS Boto3 Python 3

I have a dictionary object that is being returned to me from AWS. I need to pull the tag "based_on_ami" out of this dictionary. I have tried converting to a list, but I am new to programming and have not been able to figure out how to access Tags since they are a few levels down in the dictionary.
What is the best way for me to pull that tag out of the dictionary and put it into a variable i can use?
{
'Images':[
{
'Architecture':'x86_64',
'CreationDate':'2017-11-27T14:41:30.000Z',
'ImageId':'ami-8e73e0f4',
'ImageLocation':'23452345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5',
'ImageType':'machine',
'Public':False,
'OwnerId':'23452345234545',
'State':'available',
'BlockDeviceMappings':[
{
'DeviceName':'/dev/sda1',
'Ebs':{
'Encrypted':False,
'DeleteOnTermination':True,
'SnapshotId':'snap-0c10e8f5ced5b5240',
'VolumeSize':8,
'VolumeType':'gp2'
}
},
{
'DeviceName':'/dev/sdb',
'VirtualName':'ephemeral0'
},
{
'DeviceName':'/dev/sdc',
'VirtualName':'ephemeral1'
}
],
'EnaSupport':True,
'Hypervisor':'xen',
'Name':'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5',
'RootDeviceName':'/dev/sda1',
'RootDeviceType':'ebs',
'SriovNetSupport':'simple',
'Tags':[
{
'Key':'service',
'Value':'baseami'
},
{
'Key':'cloudservice',
'Value':'ami'
},
{
'Key':'Name',
'Value':'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5'
},
{
'Key':'os',
'Value':'ubuntu 16.04 lts'
},
{
'Key':'based_on_ami',
'Value':'ami-aa2ea8d0'
}
],
'VirtualizationType':'hvm'
}
],
'ResponseMetadata':{
'RequestId':'2c376c75-c31f-4aba-a058-173f3b125a00',
'HTTPStatusCode':200,
'HTTPHeaders':{
'content-type':'text/xml;charset=UTF-8',
'transfer-encoding':'chunked',
'vary':'Accept-Encoding',
'date':'Fri, 01 Dec 2017 18:17:53 GMT',
'server':'AmazonEC2'
},
'RetryAttempts':0
}
}
The best way to approach this type of problem is to find the value you're looking for, and then work outwards until you find a solution. You need to look at what is at each of those levels.
So, what are you looking for? You're looking for the Value for based_on_ami's Key. So your final step is going to be:
if obj['Key'] == 'based_on_ami':
# do something with obj['Value'].
But how do you get there? Well, the object is inside of a list, so you'll need to iterate the list:
for tag in <some list>:
if tag['Key'] == 'based_on_ami':
# do something with tag['Value'].
What is that list? It's the list of tags:
for tag in image['Tags']:
if tag['Key'] == 'based_on_ami':
# do something with tag['Value'].
And where are those tags? In an image object that you find in a list:
for image in image_list:
for tag in image['Tags']:
if tag['Key'] == 'based_on_ami':
# do something with tag['Value'].
The image list is the value found at the Images key in your initial dict.
image_list = my_data['Images']
for image in image_list:
for tag in image['Tags']:
if tag['Key'] == 'based_on_ami':
# do something with tag['Value'].
And now you're collecting all of those values, so you'll need a list and you'll need to append to it:
result = []
image_list = my_data['Images']
for image in image_list:
for tag in image['Tags']:
if tag['Key'] == 'based_on_ami':
result.append(tag['Value'])
So, I took your example above, and added another based_on_ami node with the value quack:
{'ResponseMetadata': {'RequestId': '2c376c75-c31f-4aba-a058-173f3b125a00', 'RetryAttempts': 0, 'HTTPHeaders': {'vary': 'Accept-Encoding', 'transfer-encoding': 'chunked', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Fri, 01 Dec 2017 18:17:53 GMT'}, 'HTTPStatusCode': 200}, 'Images': [{'Public': False, 'CreationDate': '2017-11-27T14:41:30.000Z', 'BlockDeviceMappings': [{'Ebs': {'SnapshotId': 'snap-0c10e8f5ced5b5240', 'VolumeSize': 8, 'Encrypted': False, 'VolumeType': 'gp2', 'DeleteOnTermination': True}, 'DeviceName': '/dev/sda1'}, {'VirtualName': 'ephemeral0', 'DeviceName': '/dev/sdb'}, {'VirtualName': 'ephemeral1', 'DeviceName': '/dev/sdc'}], 'OwnerId': '23452345234545', 'ImageLocation': '23452345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'RootDeviceName': '/dev/sda1', 'ImageType': 'machine', 'Hypervisor': 'xen', 'RootDeviceType': 'ebs', 'State': 'available', 'Architecture': 'x86_64', 'Name': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Tags': [{'Value': 'baseami', 'Key': 'service'}, {'Value': 'ami', 'Key': 'cloudservice'}, {'Value': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Key': 'Name'}, {'Value': 'ubuntu 16.04 lts', 'Key': 'os'}, {'Value': 'ami-aa2ea8d0', 'Key': 'based_on_ami'}], 'EnaSupport': True, 'SriovNetSupport': 'simple', 'ImageId': 'ami-8e73e0f4'}, {'Public': False, 'CreationDate': '2017-11-27T14:41:30.000Z', 'BlockDeviceMappings': [{'Ebs': {'SnapshotId': 'snap-0c10e8f5ced5b5240', 'VolumeSize': 8, 'Encrypted': False, 'VolumeType': 'gp2', 'DeleteOnTermination': True}, 'DeviceName': '/dev/sda1'}, {'VirtualName': 'ephemeral0', 'DeviceName': '/dev/sdb'}, {'VirtualName': 'ephemeral1', 'DeviceName': '/dev/sdc'}], 'VirtualizationType': 'hvm', 'OwnerId': '23452345234545', 'ImageLocation': '23452345234545/java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'RootDeviceName': '/dev/sda1', 'ImageType': 'machine', 'Hypervisor': 'xen', 'RootDeviceType': 'ebs', 'State': 'available', 'Architecture': 'x86_64', 'Name': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Tags': [{'Value': 'baseami', 'Key': 'service'}, {'Value': 'ami', 'Key': 'cloudservice'}, {'Value': 'java8server_ubuntu16-2b71edd1-f95e-4ee5-8fd6-d8a46975fdb5', 'Key': 'Name'}, {'Value': 'ubuntu 16.04 lts', 'Key': 'os'}, {'Value': 'quack', 'Key': 'based_on_ami'}], 'EnaSupport': True, 'SriovNetSupport': 'simple', 'ImageId': 'ami-8e73e0f4'}]}
My result:
['ami-aa2ea8d0', 'quack']
info = {...}
tags = []
for image in info['Images']:
for tag in image['Tags']:
if tag['Key'] == 'based_on_ami':
tags.append(tag['Value'])
print(tags)

Missing where_id in structures/<structure-id>/wheres

For one of the homes my app is connecting to using the Nest REST API, the where location assigned to one of the cameras is not listed in the wheres list. I’m not 100% sure but I believe the where location in question, ‘Front Yard’, is a built-in one that was recently added to accommodate outdoor cams. The where_id for ‘Front Yard' is L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnCbIQ0AQRHSg and for reference the complete wheres list is cut/paste below.
All of this works fine using the web app from a browser and the iOS app on on iPad/iPhone but clearly something is out of sync when using the REST API. My app only has write permission for home/away so I’m pretty I didn’t introduce the inconsistency.
Thanks,
Kevin
{'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnHRs9CoUFwCQ': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnHRs9CoUFwCQ', 'name': 'Bedroom'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrk6bmlW6JPIKw': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrk6bmlW6JPIKw', 'name': 'Den'}, 'xqmEMyhCen8FHEmFxOo5jX3RWobmscu6zBBr8pdu-veIYbhrFjxNNw': {'where_id': 'xqmEMyhCen8FHEmFxOo5jX3RWobmscu6zBBr8pdu-veIYbhrFjxNNw', 'name': 'Garage'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrlcNa-V3WRJAA': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrlcNa-V3WRJAA', 'name': 'Basement'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnH3br5CDnsKA': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnH3br5CDnsKA', 'name': 'Family Room'}, 'kJ71hVxi0hJ7zUo4TzUu3jfQLd6yPtBMx696H_tumzUUcK_3VLe0Ew': {'where_id': 'kJ71hVxi0hJ7zUo4TzUu3jfQLd6yPtBMx696H_tumzUUcK_3VLe0Ew', 'name': 'Back Yard'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmJPnCH5smMUg': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmJPnCH5smMUg', 'name': 'Hallway'}, 'd44aSYTfLVEYmcfI0Jwcz9G2uazXEmUfl_YzwLu1l37V6xivkkk1KA': {'where_id': 'd44aSYTfLVEYmcfI0Jwcz9G2uazXEmUfl_YzwLu1l37V6xivkkk1KA', 'name': 'West Yard'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrlQnDUuljAWbw': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrlQnDUuljAWbw', 'name': 'Downstairs'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrkuLsLZUeH-3A': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrkuLsLZUeH-3A', 'name': 'Master Bedroom'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrkn2yuSZHKulg': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrkn2yuSZHKulg', 'name': 'Living Room'}, '6M1qTQskUQFmQUL_QoHu-JGKdJwkoG6MZ4Bu7OsKYL-JOUQSRhhcRA': {'where_id': '6M1qTQskUQFmQUL_QoHu-JGKdJwkoG6MZ4Bu7OsKYL-JOUQSRhhcRA', 'name': 'Upstairs Hallway'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmFL6Eiqigsmw': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmFL6Eiqigsmw', 'name': 'Kids Room'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmltcwKgm79iQ': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmltcwKgm79iQ', 'name': 'Upstairs'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrn_59qioWdi-Q': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrn_59qioWdi-Q', 'name': 'Entryway'}, '3BBZNJp4tlkXpM3anwwpcnSl9t3Lqljagf0znxFq96PYyUG5_-HNGg': {'where_id': '3BBZNJp4tlkXpM3anwwpcnSl9t3Lqljagf0znxFq96PYyUG5_-HNGg', 'name': 'Guest Bedroom'}, 'fOdDlUl68hSiHsjDuNisUtq1Dq8p_SYabum4CUGUlkzJAFzMIMrgTA': {'where_id': 'fOdDlUl68hSiHsjDuNisUtq1Dq8p_SYabum4CUGUlkzJAFzMIMrgTA', 'name': 'Mud Room'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnACVy3F3e5BA': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnACVy3F3e5BA', 'name': 'Kitchen'}, '2ZK4PvWgSljwwW947D9XkLej0b7wtvDJvGG4sM_3dvEub7BoZW2Y1Q': {'where_id': '2ZK4PvWgSljwwW947D9XkLej0b7wtvDJvGG4sM_3dvEub7BoZW2Y1Q', 'name': 'East Yard'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmGc1UwaBS6nQ': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrmGc1UwaBS6nQ', 'name': 'Office'}, 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrlrBwYMB0Ma5Q': {'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrlrBwYMB0Ma5Q', 'name': 'Dining Room'}}
Looks like Nest has populated the new built-in where items - the "Front Yard" entry is now in the wheres list:
'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnCbIQ0AQRHSg': {'name': 'Front Yard', 'where_id': 'L5K8ePT_SA_xeABp03h9LOH2J3gR2sjE58zxotSsFrnCbIQ0AQRHSg'},

Resources