Exemple #1
0
def create_user():
    request = CreateUserDetails()
    request.compartment_id = compartment_id
    request.name = ''.join(
        random.sample(string.ascii_letters + string.digits, 8))
    request.description = "The user " + request.name + " was created with Python SDK"
    user = identity.create_user(request)
    return user
def create_soc_user():
    """Create a SOC user, upload public key and add the user to the SOC group"""
    # Load Public Key
    apikeydetails = CreateApiKeyDetails()
    with open(fileName, "r") as certfile:
        data = certfile.read()
    apikeydetails.key = data

    # Populate a user request
    request = CreateUserDetails()
    request.compartment_id = compartment_id
    request.name = userName
    request.description = "SOC User created For WAF Access"

    try:
        user = identity.create_user(request)
        uid = user.data.id

    except oci.exceptions.ServiceError as e:
        if e.status == 409:
            print("User '" + request.name + "' already exists.")
            structured_user_search = oci.resource_search.models.StructuredSearchDetails(
                query="query user resources where name = '" + userName + "'",
                type='Structured',
                matching_context_type=oci.resource_search.models.SearchDetails.
                MATCHING_CONTEXT_TYPE_NONE)
            results = search_client.search_resources(structured_user_search)
            for result in results.data.items:
                if debug:
                    print("User ID : " + result.identifier)
                uid = result.identifier
        else:
            print(e)
    # Upload the user's public cert for API Access
    try:
        identity.upload_api_key(uid, apikeydetails)
        print("User's public key is uploaded successfully")
    except oci.exceptions.ServiceError as e:
        if e.status == 409:
            print("User " + userName +
                  " already has a public key associated, do nothing")
        else:
            print(e)
    # Add the newly created user to the SOC Group
    try:
        add_soc_member(uid)
    except oci.exceptions.ServiceError as e:
        if e.status == 409:
            print("User" + userName +
                  " has been already added to the group :" + groupName)
        else:
            print(e)
Exemple #3
0
def create_or_update_user(identity_client, module):
    existing_user = None
    user = None
    result = dict(changed=False, user='')
    user_id = module.params.get('user_id')
    try:
        if user_id:
            existing_user = oci_utils.get_existing_resource(
                identity_client.get_user, module, user_id=user_id)
            changed, user = update_user(identity_client, existing_user, module)
            result['changed'] = changed
            result['user'] = user
        else:
            result = oci_utils.check_and_create_resource(
                resource_type='user',
                create_fn=create_user,
                kwargs_create={
                    'identity_client': identity_client,
                    'module': module
                },
                list_fn=identity_client.list_users,
                kwargs_list={
                    'compartment_id': module.params.get('compartment_id')
                },
                module=module,
                model=CreateUserDetails())
    except ServiceError as ex:
        module.fail_json(msg=ex.message)
    except MaximumWaitTimeExceeded as ex:
        module.fail_json(msg=ex.args)
    return result
Exemple #4
0
def create_user(identity_client, module):
    result = dict()
    ui_password = None
    create_user_details = CreateUserDetails()
    for attribute in create_user_details.attribute_map:
        create_user_details.__setattr__(
            attribute, module.params.get(attribute))
    result = oci_utils.create_and_wait(resource_type="user",
                                       create_fn=identity_client.create_user,
                                       kwargs_create={
                                           "create_user_details": create_user_details},
                                       client=identity_client,
                                       get_fn=identity_client.get_user,
                                       get_param="user_id",
                                       module=module,
                                       states=[
                                           module.params.get('wait_until'), "ACTIVE"]
                                       )
    user = result['user']
    user_id = user['id']
    try:
        create_password = module.params['create_or_reset_ui_password']
        if create_password:
            ui_password = create_or_reset_password(identity_client, user_id)
        groups = module.params.get('user_groups')
        if groups:
            group_ids = get_group_ids_from_group_names(
                identity_client, groups, module)
            add_user_to_groups(identity_client, user_id, group_ids)
        if ui_password:
            user.update({'password': ui_password})
    except (ServiceError, ClientError) as ex:
        message = None
        if isinstance(ex, ClientError):
            message = ex.args[0]
        else:
            message = ex.message
        module.params.update(dict({'user_id': user_id}))
        module.params.update(dict({'force': True}))
        delete_user(identity_client, module)
        module.fail_json(msg=message)

    result['user'] = user
    return result
Exemple #5
0
 def handle(self, *args, **options):
     from oci.identity import IdentityClient
     config = oci.config.from_file()
     identity = IdentityClient(config)
     from oci.identity.models import CreateGroupDetails
     request = CreateGroupDetails()
     compartment_id = config["tenancy"]
     request.compartment_id = compartment_id
     request.name = "my-test-group"
     request.description = "Created with the Python SDK"
     group = identity.create_group(request)
     from oci.identity.models import CreateUserDetails
     request = CreateUserDetails()
     request.compartment_id = compartment_id
     request.name = "my-test-user"
     request.description = "Created with the Python SDK"
     user = identity.create_user(request)
     from oci.identity.models import AddUserToGroupDetails
     request = AddUserToGroupDetails()
     request.group_id = group.data.id
     request.user_id = user.data.id
     response = identity.add_user_to_group(request)
     print(response.status)
Exemple #6
0
# Get and set the home region for the compartment. User crud operations need
# to be performed in the home region.
response = identity.list_region_subscriptions(compartment_id)
for region in response.data:
    if region.is_home_region:
        identity.base_client.set_region(region.region_name)
        break

user_name = "python-sdk-example-user"
group_name = "python-sdk-example-group"

print("Creating a new user {!r} in compartment {!r}".format(
    user_name, compartment_id))

request = CreateUserDetails()
request.compartment_id = compartment_id
request.name = user_name
request.description = "Created by a Python SDK example"
user = identity.create_user(request)
print(user.data)

print("Creating a new group {!r} in compartment {!r}".format(
    group_name, compartment_id))

request = CreateGroupDetails()
request.compartment_id = compartment_id
request.name = group_name
request.description = "Created by a Python SDK example"
group = identity.create_group(request)
print(group.data)
Exemple #7
0
# Get and set the home region for the compartment. User crud operations need
# to be performed in the home region.
response = identity.list_region_subscriptions(compartment_id)
for region in response.data:
    if region.is_home_region:
        identity.base_client.set_region(region.region_name)
        break

user_name = "python-sdk-example-user"
group_name = "python-sdk-example-group"

print("Creating a new user {!r} in compartment {!r}".format(
    user_name, compartment_id))

request = CreateUserDetails()
request.compartment_id = compartment_id
request.name = user_name
request.description = "Created by a Python SDK example"
user = identity.create_user(request)
print(user.data)

print("Creating a new group {!r} in compartment {!r}".format(
    group_name, compartment_id))

request = CreateGroupDetails()
request.compartment_id = compartment_id
request.name = group_name
request.description = "Created by a Python SDK example"
group = identity.create_group(request)
print(group.data)
Exemple #8
0
    else:
        print("Running with: ")
        print("Username: {}".format(sys.argv[1]))
        print("")


#
# Main
#
print("")

controlla_params()

validate_config(config)

print("Validate config OK")

username = sys.argv[1]

client = IdentityClient(config)

# set user details
details = CreateUserDetails(compartment_id=config.get("tenancy"),
                            description="ocipy_created",
                            name=username)

client.create_user(details)

print("User created !")
print("")
Exemple #9
0
# Creating an OCI user in Python
import oci
config = oci.config.from_file()
identity_client = oci.identity.IdentityClient(config)
compartment_id = config["tenancy"]

#prepare OCI request by assigning the desired user parameters
from oci.identity.models import CreateUserDetails
request = CreateUserDetails()
request.compartment_id = compartment_id
request.name = "python-user"
request.description = "Created with the Python SDK"
user = identity_client.create_user(request)
print(user.data.id)
Exemple #10
0
import oci
import os
from oci.identity.models import CreateUserDetails

os.environ['http_proxy'] = 'http://www-proxy-hqdc.us.oracle.com:80'
os.environ['https_proxy'] = 'http://www-proxy-hqdc.us.oracle.com:80'


config=oci.config.from_file()
identity=oci.identity.IdentityClient(config)

compartment_id=config["tenancy"]

request = CreateUserDetails()
request.compartment_id=compartment_id
request.name="my-test-user-sdk-1"
request.description="Created with Python SDK1"
user=identity.create_user(request)
print(user.data)