|
| 1 | +#!/usr/bin/python |
| 2 | +# coding=utf-8 |
| 3 | + |
| 4 | +from openstack import connection |
| 5 | + |
| 6 | +username = "**********" |
| 7 | +password = "**********" |
| 8 | +userDomainId = "**********" |
| 9 | +auth_url = "**********" |
| 10 | + |
| 11 | +# create connection |
| 12 | +conn = connection.Connection( |
| 13 | + auth_url=auth_url, |
| 14 | + user_domain_id=userDomainId, |
| 15 | + domain_id=userDomainId, |
| 16 | + username=username, |
| 17 | + password=password |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +# Query an agency list |
| 22 | +# GET /v3.0/OS-AGENCY/agencies |
| 23 | +def get_agency_list(): |
| 24 | + agencies = conn.iam.agencies(domain_id=userDomainId) |
| 25 | + # agencies = conn.iam.agencies(domain_id=userDomainId, name="**********", trust_domain_id="**********") |
| 26 | + for agency in agencies: |
| 27 | + print(agency) |
| 28 | + |
| 29 | + |
| 30 | +# Query agency details |
| 31 | +# GET /v3.0/OS-AGENCY/agencies/{agency_id} |
| 32 | +def get_agency(): |
| 33 | + agency_id = "**********" |
| 34 | + agency = conn.iam.get_agency(agency_id) |
| 35 | + print(agency) |
| 36 | + |
| 37 | + |
| 38 | +# Create agency |
| 39 | +# POST /v3.0/OS-AGENCY/agencies |
| 40 | +def create_agency(): |
| 41 | + agency = { |
| 42 | + "name": "**********", |
| 43 | + "domain_id": "**********", |
| 44 | + "trust_domain_id": "**********", |
| 45 | + "trust_domain_name": "**********", |
| 46 | + "duration": "**********", |
| 47 | + "description": "**********" |
| 48 | + } |
| 49 | + agency = conn.iam.create_agency(**agency) |
| 50 | + print(agency) |
| 51 | + |
| 52 | + |
| 53 | +# Update agency |
| 54 | +# POST /v3.0/OS-AGENCY/agencies |
| 55 | +def update_agency(): |
| 56 | + agency_id = "**********" |
| 57 | + agency = { |
| 58 | + "agency": { |
| 59 | + "trust_domain_id": "**********", |
| 60 | + "trust_domain_name": "**********", |
| 61 | + "description": "**********", |
| 62 | + "duration": "**********" |
| 63 | + } |
| 64 | + } |
| 65 | + agency = conn.iam.update_agency(agency_id, **agency) |
| 66 | + print(agency) |
| 67 | + |
| 68 | + |
| 69 | +# Delete agency |
| 70 | +# DELETE /v3.0/OS-CREDENTIAL/credentials/{access_key} |
| 71 | +def delete_agency(): |
| 72 | + agency_id = "**********" |
| 73 | + conn.iam.delete_agency(agency_id) |
| 74 | + |
| 75 | + |
| 76 | +# Query domain permissions of an agency |
| 77 | +# GET /v3.0/OS-AGENCY/domains/{domain_id}/agencies/{agency_id}/roles |
| 78 | +def list_domain_agency_role(): |
| 79 | + domain_id = "**********" |
| 80 | + agency_id = "**********" |
| 81 | + roles = conn.iam.list_domain_agency_role(domain_id, agency_id) |
| 82 | + for role in roles: |
| 83 | + print(role) |
| 84 | + |
| 85 | + |
| 86 | +# Query project permissions of an agency |
| 87 | +# GET /v3.0/OS-AGENCY/projects/{project_id}/agencies/{agency_id}/roles |
| 88 | +def list_project_agency_role(): |
| 89 | + project_id = "**********" |
| 90 | + agency_id = "**********" |
| 91 | + roles = conn.iam.list_project_agency_role(project_id, agency_id) |
| 92 | + for role in roles: |
| 93 | + print(role) |
| 94 | + |
| 95 | + |
| 96 | +# Grant domain permission to an agency |
| 97 | +# PUT /v3.0/OS-AGENCY/domains/{domain_id}/agencies/{agency_id}/roles/{role_id} |
| 98 | +def grant_domain_agency_role(): |
| 99 | + domain_id = "**********" |
| 100 | + agency_id = "**********" |
| 101 | + role_id = "**********" |
| 102 | + result = conn.iam.grant_domain_agency_role(domain_id, agency_id, role_id) |
| 103 | + if result is True: |
| 104 | + print("Grant domain permission to an agency successfully") |
| 105 | + else: |
| 106 | + print("Grant domain permission to an agency failure") |
| 107 | + |
| 108 | + |
| 109 | +# Grant project permission to an agency |
| 110 | +# PUT /v3.0/OS-AGENCY/projects/{project_id}/agencies/{agency_id}/roles/{role_id} |
| 111 | +def grant_project_agency_role(): |
| 112 | + project_id = "**********" |
| 113 | + agency_id = "**********" |
| 114 | + role_id = "**********" |
| 115 | + result = conn.iam.grant_project_agency_role(project_id, agency_id, role_id) |
| 116 | + if result is True: |
| 117 | + print("Grant project permission to an agency successfully") |
| 118 | + else: |
| 119 | + print("Grant project permission to an agency failure") |
| 120 | + |
| 121 | + |
| 122 | +# Query whether an agency has specific domain permission |
| 123 | +# HEAD /v3.0/OS-AGENCY/domains/{domain_id}/agencies/{agency_id}/roles/{role_id} |
| 124 | +def check_domain_agency_role(): |
| 125 | + domain_id = "**********" |
| 126 | + agency_id = "**********" |
| 127 | + role_id = "**********" |
| 128 | + result = conn.iam.check_domain_agency_role(domain_id, agency_id, role_id) |
| 129 | + if result is True: |
| 130 | + print("The agency has this domain permission") |
| 131 | + else: |
| 132 | + print("The agency doesn't have this domain permission") |
| 133 | + |
| 134 | + |
| 135 | +# Query whether an agency has specific project permission |
| 136 | +# HEAD /v3.0/OS-AGENCY/projects/{project_id}/agencies/{agency_id}/roles/{role_id} |
| 137 | +def check_project_agency_role(): |
| 138 | + project_id = "**********" |
| 139 | + agency_id = "**********" |
| 140 | + role_id = "**********" |
| 141 | + result = conn.iam.check_project_agency_role(project_id, agency_id, role_id) |
| 142 | + if result is True: |
| 143 | + print("The agency has this project permission") |
| 144 | + else: |
| 145 | + print("The agency doesn't have this project permission") |
| 146 | + |
| 147 | + |
| 148 | +# Delete domain permission of an agency |
| 149 | +# DELETE /v3.0/OS-AGENCY/domains/{domain_id}/agencies/{agency_id}/roles/{role_id} |
| 150 | +def delete_domain_agency_role(): |
| 151 | + domain_id = "**********" |
| 152 | + agency_id = "**********" |
| 153 | + role_id = "**********" |
| 154 | + result = conn.iam.delete_domain_agency_role(domain_id, agency_id, role_id) |
| 155 | + if result is True: |
| 156 | + print("Delete domain permission of an agency successfully") |
| 157 | + else: |
| 158 | + print("Delete domain permission of an agency failure") |
| 159 | + |
| 160 | + |
| 161 | +# Delete project permission of an agency |
| 162 | +# DELETE /v3.0/OS-AGENCY/projects/{project_id}/agencies/{agency_id}/roles/{role_id} |
| 163 | +def delete_project_agency_role(): |
| 164 | + project_id = "**********" |
| 165 | + agency_id = "**********" |
| 166 | + role_id = "**********" |
| 167 | + result = conn.iam.delete_project_agency_role(project_id, agency_id, role_id) |
| 168 | + if result is True: |
| 169 | + print("Delete project permission of an agency successfully") |
| 170 | + else: |
| 171 | + print("Delete project permission of an agency failure") |
| 172 | + |
| 173 | + |
| 174 | +if __name__ == "__main__": |
| 175 | + get_agency_list() |
| 176 | + get_agency() |
| 177 | + create_agency() |
| 178 | + update_agency() |
| 179 | + delete_agency() |
| 180 | + list_domain_agency_role() |
| 181 | + list_project_agency_role() |
| 182 | + grant_domain_agency_role() |
| 183 | + grant_project_agency_role() |
| 184 | + check_domain_agency_role() |
| 185 | + check_project_agency_role() |
| 186 | + delete_domain_agency_role() |
| 187 | + delete_project_agency_role() |
0 commit comments