Skip to content

Commit c9df540

Browse files
authored
feat(IAM Policy Management): add support for policy templates (#203)
Signed-off-by: Shaun Colley <[email protected]>
1 parent 5e5670d commit c9df540

File tree

4 files changed

+7756
-2005
lines changed

4 files changed

+7756
-2005
lines changed

examples/test_iam_policy_management_v1_examples.py

Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
example_policy_etag = None
5050
example_custom_role_id = None
5151
example_custom_role_etag = None
52+
example_template_id = None
53+
example_template_etag = None
54+
example_template_version = None
55+
example_assignment_id = None
5256
example_user_id = "IBMid-user1"
5357
example_service_name = "iam-groups"
5458

@@ -543,6 +547,341 @@ def test_delete_role_example(self):
543547
except ApiException as e:
544548
pytest.fail(str(e))
545549

550+
@needscredentials
551+
def test_create_policy_template_example(self):
552+
"""
553+
create_policy_template request example
554+
"""
555+
try:
556+
print('\ncreate_policy_template() result:')
557+
558+
global example_template_id
559+
global example_template_version
560+
# begin-create_policy_template
561+
562+
v2_policy_resource_attribute_model = {
563+
'key': 'serviceType',
564+
'operator': 'stringEquals',
565+
'value': 'service',
566+
}
567+
568+
v2_policy_resource_model = {
569+
'attributes': [v2_policy_resource_attribute_model],
570+
}
571+
572+
roles_model = {
573+
'role_id': 'crn:v1:bluemix:public:iam::::role:Viewer',
574+
}
575+
576+
grant_model = {
577+
'roles': [roles_model],
578+
}
579+
580+
control_model = {
581+
'grant': grant_model,
582+
}
583+
584+
template_policy_model = {
585+
'type': 'access',
586+
'resource': v2_policy_resource_model,
587+
'control': control_model,
588+
}
589+
590+
response = iam_policy_management_service.create_policy_template(
591+
name='SDKExamplesTest',
592+
account_id=example_account_id,
593+
policy=template_policy_model,
594+
)
595+
policy_template = response.get_result()
596+
597+
print(json.dumps(policy_template, indent=2))
598+
599+
# end-create_policy_template
600+
601+
example_template_id = policy_template['id']
602+
example_template_version = policy_template['version']
603+
604+
except ApiException as e:
605+
pytest.fail(str(e))
606+
607+
@needscredentials
608+
def test_get_policy_template_example(self):
609+
"""
610+
get_policy_template request example
611+
"""
612+
try:
613+
print('\nget_policy_template() result:')
614+
615+
global example_template_etag
616+
# begin-get_policy_template
617+
618+
print('example_template_id: ', example_template_id)
619+
response = iam_policy_management_service.get_policy_template(
620+
policy_template_id=example_template_id,
621+
)
622+
policy_template = response.get_result()
623+
624+
print(json.dumps(policy_template, indent=2))
625+
626+
# end-get_policy_template
627+
628+
example_template_etag = response.get_headers().get("Etag")
629+
630+
except ApiException as e:
631+
pytest.fail(str(e))
632+
633+
@needscredentials
634+
def test_replace_policy_template_example(self):
635+
"""
636+
replace_policy_template request example
637+
"""
638+
try:
639+
print('\nreplace_policy_template() result:')
640+
# begin-replace_policy_template
641+
642+
v2_policy_resource_attribute_model = {
643+
'key': 'serviceType',
644+
'operator': 'stringEquals',
645+
'value': 'service',
646+
}
647+
648+
v2_policy_resource_model = {
649+
'attributes': [v2_policy_resource_attribute_model],
650+
}
651+
652+
roles_model = {
653+
'role_id': 'crn:v1:bluemix:public:iam::::role:Editor',
654+
}
655+
656+
grant_model = {
657+
'roles': [roles_model],
658+
}
659+
660+
control_model = {
661+
'grant': grant_model,
662+
}
663+
664+
template_policy_model = {
665+
'type': 'access',
666+
'resource': v2_policy_resource_model,
667+
'control': control_model,
668+
}
669+
670+
response = iam_policy_management_service.replace_policy_template(
671+
policy_template_id=example_template_id,
672+
version=example_template_version,
673+
if_match=example_template_etag,
674+
policy=template_policy_model,
675+
)
676+
policy_template = response.get_result()
677+
678+
print(json.dumps(policy_template, indent=2))
679+
680+
# end-replace_policy_template
681+
682+
except ApiException as e:
683+
pytest.fail(str(e))
684+
685+
@needscredentials
686+
def test_list_policy_templates_example(self):
687+
"""
688+
list_policy_templates request example
689+
"""
690+
try:
691+
print('\nlist_policy_templates() result:')
692+
# begin-list_policy_templates
693+
694+
response = iam_policy_management_service.list_policy_templates(
695+
account_id=example_account_id,
696+
)
697+
policy_template_collection = response.get_result()
698+
699+
print(json.dumps(policy_template_collection, indent=2))
700+
701+
# end-list_policy_templates
702+
703+
except ApiException as e:
704+
pytest.fail(str(e))
705+
706+
@needscredentials
707+
def test_create_policy_template_version_example(self):
708+
"""
709+
create_policy_template_version request example
710+
"""
711+
try:
712+
print('\ncreate_policy_template_version() result:')
713+
# begin-create_policy_template_version
714+
715+
v2_policy_resource_attribute_model = {
716+
'key': 'serviceType',
717+
'operator': 'stringEquals',
718+
'value': 'service',
719+
}
720+
721+
v2_policy_resource_model = {
722+
'attributes': [v2_policy_resource_attribute_model],
723+
}
724+
725+
roles_model = {
726+
'role_id': 'crn:v1:bluemix:public:iam::::role:Viewer',
727+
}
728+
729+
grant_model = {
730+
'roles': [roles_model],
731+
}
732+
733+
control_model = {
734+
'grant': grant_model,
735+
}
736+
737+
template_policy_model = {
738+
'type': 'access',
739+
'resource': v2_policy_resource_model,
740+
'control': control_model,
741+
}
742+
743+
response = iam_policy_management_service.create_policy_template_version(
744+
policy_template_id=example_template_id,
745+
policy=template_policy_model,
746+
)
747+
policy_template = response.get_result()
748+
749+
print(json.dumps(policy_template, indent=2))
750+
751+
# end-create_policy_template_version
752+
753+
except ApiException as e:
754+
pytest.fail(str(e))
755+
756+
@needscredentials
757+
def test_list_policy_template_versions_example(self):
758+
"""
759+
list_policy_template_versions request example
760+
"""
761+
try:
762+
print('\nlist_policy_template_versions() result:')
763+
# begin-list_policy_template_versions
764+
765+
response = iam_policy_management_service.list_policy_template_versions(
766+
policy_template_id=example_template_id,
767+
)
768+
policy_template_versions_collection = response.get_result()
769+
770+
print(json.dumps(policy_template_versions_collection, indent=2))
771+
772+
# end-list_policy_template_versions
773+
774+
except ApiException as e:
775+
pytest.fail(str(e))
776+
777+
@needscredentials
778+
def test_get_policy_template_version_example(self):
779+
"""
780+
get_policy_template_version request example
781+
"""
782+
try:
783+
print('\nget_policy_template_version() result:')
784+
# begin-get_policy_template_version
785+
786+
response = iam_policy_management_service.get_policy_template_version(
787+
policy_template_id=example_template_id,
788+
version=example_template_version,
789+
)
790+
policy_template = response.get_result()
791+
792+
print(json.dumps(policy_template, indent=2))
793+
794+
# end-get_policy_template_version
795+
796+
example_template_etag = response.get_headers().get("Etag")
797+
798+
except ApiException as e:
799+
pytest.fail(str(e))
800+
801+
@needscredentials
802+
def test_commit_policy_template_example(self):
803+
"""
804+
commit_policy_template request example
805+
"""
806+
try:
807+
# begin-commit_policy_template
808+
809+
response = iam_policy_management_service.commit_policy_template(
810+
policy_template_id=example_template_id,
811+
version=example_template_version,
812+
if_match=example_template_etag,
813+
)
814+
815+
# end-commit_policy_template
816+
print('\ncommit_policy_template() response status code: ', response.get_status_code())
817+
818+
except ApiException as e:
819+
pytest.fail(str(e))
820+
821+
@needscredentials
822+
def test_list_policy_assignments_example(self):
823+
"""
824+
list_policy_assignments request example
825+
"""
826+
try:
827+
print('\nlist_policy_assignments() result:')
828+
829+
global example_assignment_id
830+
# begin-list_Policy Assignments
831+
832+
response = iam_policy_management_service.list_policy_assignments(
833+
account_id=example_account_id,
834+
)
835+
polcy_template_assignment_collection = response.get_result()
836+
837+
print(json.dumps(polcy_template_assignment_collection, indent=2))
838+
839+
# end-list_Policy Assignments
840+
841+
example_assignment_id = polcy_template_assignment_collection['assignments'][0]['id']
842+
843+
except ApiException as e:
844+
pytest.fail(str(e))
845+
846+
@needscredentials
847+
def test_get_policy_assignment_example(self):
848+
"""
849+
get_policy_assignment request example
850+
"""
851+
try:
852+
print('\nget_policy_assignment() result:')
853+
# begin-get_policy_assignment
854+
855+
response = iam_policy_management_service.get_policy_assignment(
856+
assignment_id=example_assignment_id,
857+
)
858+
policy_assignment_record = response.get_result()
859+
860+
print(json.dumps(policy_assignment_record, indent=2))
861+
862+
# end-get_policy_assignment
863+
864+
except ApiException as e:
865+
pytest.fail(str(e))
866+
867+
@needscredentials
868+
def test_delete_policy_template_example(self):
869+
"""
870+
delete_policy_template request example
871+
"""
872+
try:
873+
# begin-delete_policy_template
874+
875+
response = iam_policy_management_service.delete_policy_template(
876+
policy_template_id=example_template_id,
877+
)
878+
879+
# end-delete_policy_template
880+
print('\ndelete_policy_template() response status code: ', response.get_status_code())
881+
882+
except ApiException as e:
883+
pytest.fail(str(e))
884+
546885

547886
# endregion
548887
##############################################################################

0 commit comments

Comments
 (0)