Ejemplo n.º 1
0
def main():
    module = AnsibleModule(argument_spec=dict(
        state=dict(choices=['present', 'absent'], default='present'),
        token=dict(type='str', no_log=True),
    ),
                           supports_check_mode=True)

    state = module.params.get('state')

    if module.params['token']:
        token = module.params['token']
    else:
        token = os.environ.get('SEL_TOKEN')
    url = os.environ.get('SEL_URL')

    # Configure REST client
    try:
        http_client = setup_http_client(url,
                                        api_token=token,
                                        custom_headers=custom_user_agent)
        client = Client(http_client)
    except Exception:
        module.fail_json(msg="No token given")

    if module.check_mode:
        module.exit_json(changed=False)

    if state == 'present':
        try:
            result = client.capabilities.get()
        except ClientException as exp:
            module.fail_json(msg=str(exp))
        module.exit_json(result=result._info)
    module.fail_json(msg="Wrong 'state' param for 'capabilities' operation.")
Ejemplo n.º 2
0
def CreateProject(project):
    http_client = setup_http_client(
        api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    project1 = selvpc.projects.create(project['name'],
                                      project['quatas'],
                                      return_raw=True)
def main():
    module = AnsibleModule(argument_spec=dict(state=dict(
        choices=['present', 'absent'], default='present'),
                                              token=dict(type='str',
                                                         no_log=True),
                                              list=dict(type='bool',
                                                        default=False),
                                              project_name=dict(type='str'),
                                              project_id=dict(type='str'),
                                              licenses=dict(type='list'),
                                              license_id=dict(type='str'),
                                              detailed=dict(type='bool',
                                                            default=False),
                                              force=dict(type='bool',
                                                         default=False)),
                           supports_check_mode=True)

    project_id = module.params.get('project_id')
    state = module.params.get('state')
    show_list = module.params.get('list')
    licenses = module.params.get('licenses')
    license_id = module.params.get('license_id')
    detailed = module.params.get('detailed')
    project_name = module.params.get('project_name')
    force = module.params.get('force')

    if module.params['token']:
        token = module.params['token']
    else:
        token = os.environ.get('SEL_TOKEN')
    url = os.environ.get('SEL_URL')

    # Configure REST client
    try:
        http_client = setup_http_client(url,
                                        api_token=token,
                                        custom_headers=custom_user_agent)
        client = Client(http_client)
    except Exception:
        module.fail_json(msg="No token given")

    if module.check_mode:
        module.exit_json(changed=_system_state_change(module, client))

    if state == "absent" and license_id:
        lic.delete_license(module, client, license_id)

    if state == "present":
        if licenses and (project_id or project_name):
            lic.add_licenses(module, client, project_id, project_name,
                             licenses, force)

        if license_id and not show_list or show_list:
            lic.get_licenses(module,
                             client,
                             license_id,
                             detailed,
                             show_list=show_list)
    module.fail_json(msg="No params for 'licenses' operations.")
Ejemplo n.º 4
0
def DeleteUser(id):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    try:
        user=selvpc.users.delete(id)
        return user
    except:
        return -1
Ejemplo n.º 5
0
def CreateUser(name, password):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    try:
        user=selvpc.users.create(name,password,enabled=True,return_raw=True)
        return user
    except:
        return -1
Ejemplo n.º 6
0
def CreateProject(name,quotas):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    if quotas=='0':
        project1 = selvpc.projects.create(name,return_raw=True)
    else:
        project1 = selvpc.projects.create(name,quotas,return_raw=True)
    return project1
Ejemplo n.º 7
0
def CreateIp_floating():
    REGION = "ru-1"
    http_client = setup_http_client(
        api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    project = selvpc.projects.list()[0]
    floating_ips = {"floatingips": [{"region": REGION, "quantity": 1}]}
    project.add_floating_ips(floating_ips)
Ejemplo n.º 8
0
def Add_Ip(name, ip):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    project=selvpc.projects.list()
    for l in project:
        if l['name']==name:
            answer=l.add_floating_ips(ip, return_raw=True)[0]
            return answer;
    return -1
Ejemplo n.º 9
0
def Add_Subset(name, subnet):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    project=selvpc.projects.list()
    for l in project:
        if l['name']==name:
            answer=l.add_subnet(subnet,return_raw=True)
            return answer;
    return -1
Ejemplo n.º 10
0
def DeleteProject(id):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    try:
     project1 = selvpc.projects.delete(id)
   
     return project1
    except:
        return -1
Ejemplo n.º 11
0
def Delete_Ip(ip):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
   
    try:
        answer=selvpc.floatingips.delete(ip['id'],return_raw=True)
        return answer
    except:
       
        return -1
Ejemplo n.º 12
0
def Delete_Project(project):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
   
    try:
        answer=selvpc.projects.delete(project['id'])
        return answer
    except:
       
        return -1
Ejemplo n.º 13
0
def Delete_Subset(subnet):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
   
    try:
        answer=selvpc.subnets.delete(subnet['id'],return_raw=True)
        return answer
    except:
       
        return -1
Ejemplo n.º 14
0
def Add_user_in_project(id,name):
    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    user = selvpc.users.list()
    for i in user:
        if i['name']==name:
            user=i.add_to_project(id,return_raw=True)
            return user
    else:
        return -1
Ejemplo n.º 15
0
def main():
    module = AnsibleModule(argument_spec=dict(state=dict(
        choices=['present', 'absent'], default='present'),
                                              token=dict(type='str',
                                                         no_log=True),
                                              username=dict(type='str'),
                                              password=dict(type='str',
                                                            no_log=True),
                                              new_username=dict(type='str'),
                                              new_password=dict(type='str',
                                                                no_log=True),
                                              user_id=dict(type='str'),
                                              enabled=dict(type='bool',
                                                           default=True)),
                           supports_check_mode=True)

    state = module.params.get('state')
    username = module.params.get('username')
    password = module.params.get('password')
    new_username = module.params.get('new_username')
    new_password = module.params.get('new_password')
    user_id = module.params.get('user_id')
    enabled = module.params.get('enabled')

    if module.params['token']:
        token = module.params['token']
    else:
        token = os.environ.get('SEL_TOKEN')
    url = os.environ.get('SEL_URL')

    # Configure REST client
    try:
        http_client = setup_http_client(url,
                                        api_token=token,
                                        custom_headers=custom_user_agent)
        client = Client(http_client)
    except Exception:
        module.fail_json(msg="No token given")

    if module.check_mode:
        module.exit_json(changed=_system_state_change(module, client))

    if state == 'absent' and user_id:
        delete_user(module, client, user_id)

    if state == 'present':
        if username and password:
            create_user(module, client, username, password, enabled)

        if user_id and (new_username or new_password) or (new_username
                                                          and new_password):
            update_user(module, client, user_id, new_username, new_password,
                        enabled)
        get_users(module, client)
    module.fail_json(msg="No params for 'users' operations.")
Ejemplo n.º 16
0
def main():
    module = AnsibleModule(argument_spec=dict(
        state=dict(choices=['present', 'absent'], default='present'),
        token=dict(type='str', no_log=True),
        list=dict(type='bool', default=False),
        project_name=dict(type='str'),
        project_id=dict(type='str'),
        new_name=dict(type='str'),
        quotas=dict(type='dict'),
    ),
                           supports_check_mode=True)

    if module.params['token']:
        token = module.params['token']
    else:
        token = os.environ.get('SEL_TOKEN')
    url = os.environ.get('SEL_URL')

    # Configure REST client
    try:
        http_client = setup_http_client(url,
                                        api_token=token,
                                        custom_headers=custom_user_agent)
        client = Client(http_client)
    except Exception:
        module.fail_json(msg="No token given")

    if module.check_mode:
        module.exit_json(changed=_system_state_change(module, client))

    project_name = module.params.get('project_name')
    project_id = module.params.get('project_id')
    state = module.params.get('state')
    show_list = module.params.get('list')
    new_name = module.params.get('new_name')

    if state == 'absent' and (project_id or project_name):
        p.delete_project(module, client, project_id, project_name)

    if state == "present":

        if new_name and (project_id or project_name):
            p.update_project(module, client, project_id, project_name,
                             new_name)

        if project_name and not c.get_project_by_name(client, project_name):
            p.create_project(module, client, project_name)

        if ((project_id or project_name) and not show_list) or show_list:
            p.get_project(module,
                          client,
                          project_id,
                          project_name,
                          show_list=show_list)
    module.fail_json(msg="No params for 'selvpc_projects' operations.")
Ejemplo n.º 17
0
def UpdateQoatas(Qoates):

    REGION = "ru-1"
    ZONE = "ru-1a"

    http_client = setup_http_client(
        api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)

    Quatas = project.update_quotas(Qoates)

    return Quatas
Ejemplo n.º 18
0
def make_client(return_value):
    response = mock.Mock()
    response.json = mock.Mock(return_value=return_value)
    http_client = mock.Mock()
    methods = {
        "get.return_value": response,
        "post.return_value": response,
        "patch.return_value": response,
        "put.return_value": response,
        "delete.return_value": response,
    }
    http_client.configure_mock(**methods)
    return Client(client=http_client)
Ejemplo n.º 19
0
def main():
    module = AnsibleModule(argument_spec=dict(state=dict(
        choices=['present', 'absent'], default='present'),
                                              token=dict(type='str',
                                                         no_log=True),
                                              project_id=dict(type='str'),
                                              user_id=dict(type='str'),
                                              roles=dict(type='list'),
                                              project_name=dict(type='str')),
                           supports_check_mode=True)

    project_id = module.params.get('project_id')
    state = module.params.get('state')
    user_id = module.params.get('user_id')
    roles = module.params.get('roles')
    project_name = module.params.get('project_name')

    if module.params['token']:
        token = module.params['token']
    else:
        token = os.environ.get('SEL_TOKEN')
    url = os.environ.get('SEL_URL')

    # Configure REST client
    try:
        http_client = setup_http_client(url,
                                        api_token=token,
                                        custom_headers=custom_user_agent)
        client = Client(http_client)
    except Exception:
        module.fail_json(msg="No token given")

    if module.check_mode:
        module.exit_json(changed=_system_state_change(module, client))

    if state == 'absent' and (project_id or project_name) and user_id:
        r.delete_role(module, client, project_id, project_name, user_id)

    if state == 'present':
        if user_id and (project_id or project_name):
            r.add_role(module, client, project_id, project_name, user_id)

        if roles:
            r.add_bulk_roles(module, client, roles)

        if user_id:
            r.get_user_roles(module, client, user_id)

        if project_id or project_name:
            r.get_project_roles(module, client, project_id, project_name)
    module.fail_json(msg="No params for 'roles' operations.")
Ejemplo n.º 20
0
def UpdateQoatas(name,Qoates):

    REGION = "ru-1"
    ZONE = "ru-1a"

    http_client = setup_http_client(api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    project=selvpc.projects.list()
    for l in project:
        if l['name']==name:
            quotas=l.update_quotas(Qoates)
            return quotas

    return -1
Ejemplo n.º 21
0
    def prepare_to_run_command(self, cmd):
        if cmd.cmd_name == 'complete':
            return

        if not self.options.url:
            logger.error("argument --url is required")
            sys.exit(1)

        headers = None
        if self.options.header:
            headers = parse_headers(self.options.header)

        http_client = setup_http_client(api_url=self.options.url,
                                        api_version=self.options.api_version,
                                        api_token=self.options.token,
                                        custom_headers=headers)
        self.context = dict(client=Client(client=http_client))
def main():
    module = AnsibleModule(argument_spec=dict(
        state=dict(choices=['present', 'absent'], default='present'),
        token=dict(type='str', no_log=True),
        project_id=dict(type='str'),
        project_name=dict(type='str')
    ), supports_check_mode=True)

    project_id = module.params.get('project_id')
    state = module.params.get('state')
    project_name = module.params.get('project_name')

    if module.params['token']:
        token = module.params['token']
    else:
        token = os.environ.get('SEL_TOKEN')
    url = os.environ.get('SEL_URL')

    # Configure REST client
    try:
        http_client = setup_http_client(url,
                                        api_token=token,
                                        custom_headers=custom_user_agent)
        client = Client(http_client)
    except Exception:
        module.fail_json(msg="No token given")

    if module.check_mode:
        module.exit_json(changed=_system_state_change(module, client))

    if state == 'present' and (project_id or project_name):
        try:
            if not project_id:
                project = get_project_by_name(client, project_name)
                if not project:
                    raise ClientException(message="No such project")
                project_id = project.id
            client.tokens.create(project_id)
        except ClientException as exp:
            module.fail_json(msg=str(exp))
        module.exit_json(changed=True, result="Token has been created")
    elif state == 'absent' and project_id:
        module.fail_json(msg="Wrong 'state' for 'tokens' operations.")
    module.fail_json(msg="No params for 'tokens' operations.")
Ejemplo n.º 23
0
    def prepare_to_run_command(self, cmd):
        # NOTE: cliff earlier 2.8 doesn't fill "internal" commands.
        if not cmd.cmd_name or cmd.cmd_name in ['complete', 'help']:
            return

        if not self.options.url:
            logger.error("argument --url is required")
            sys.exit(1)

        headers = None
        if self.options.header:
            headers = parse_headers(self.options.header)

        http_client = setup_http_client(
            api_url=self.options.url,
            api_version=self.options.api_version,
            api_token=self.options.token,
            custom_headers=headers,
            timeout=self.options.timeout,
        )
        self.context = dict(client=Client(client=http_client))
def main():
    module = AnsibleModule(argument_spec=dict(
        state=dict(choices=['present', 'absent'], default='present'),
        token=dict(type='str', no_log=True),
        free=dict(type='bool', default=False),
    ),
                           supports_check_mode=True)

    if module.params['token']:
        token = module.params['token']
    else:
        token = os.environ.get('SEL_TOKEN')
    url = os.environ.get('SEL_URL')

    # Configure REST client
    try:
        http_client = setup_http_client(url,
                                        api_token=token,
                                        custom_headers=custom_user_agent)
        client = Client(http_client)
    except Exception:
        module.fail_json(msg="No token given")

    state = module.params.get('state')
    free = module.params.get('free')

    if module.check_mode:
        module.exit_json(changed=False)

    if state == "present":
        if free:
            get_free_domain_quotas(module, client)
        get_domain_quotas(module, client)

    if state == "absent":
        module.fail_json(msg="Wrong state for 'selvpc_limits' operations.")
    module.fail_json(msg="No params for 'selvpc_limits' operations.")
Ejemplo n.º 25
0
def UpdateQoatas(Qoates):

    REGION = "ru-1"
    ZONE = "ru-1a"

    http_client = setup_http_client(
        api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    '''
    project = selvpc.projects.create(name="Awesome Projectq12")
    logging.info("Project '{}' has been created with id '{}'".format(project.name,
                                                                 project.id))

    quotas = {
        "quotas": {
            "compute_cores": [
                {
                "region": REGION,
                "zone": ZONE,
                "value": 1
            }
        ],
        "compute_ram": [
            {
                "region": REGION,
                "zone": ZONE,
                "value": 512
            }
        ],
        "volume_gigabytes_fast": [
            {
                "region": REGION,
                "zone": ZONE,
                "value": 5
            }
        ]
        }
    }

    project.update_quotas(quotas)
    logging.info("Project quotas has been set")

    floating_ips = {
        "floatingips": [
            {
                "region": REGION,
                "quantity": 1
            }
        ]
    }

    ip = project.add_floating_ips(floating_ips)[0]
    logging.info("IP {} has been added".format(ip.floating_ip_address))
    
    quotas = {
        "quotas": {
            "compute_cores": [
                {
                    "region": REGION,
                    "zone": ZONE,
                    "value": 20
                }
            ],
            "compute_ram": [
                {
                    "region": REGION,
                    "zone": ZONE,
                    "value": 2048
                }
            ],
            "volume_gigabytes_fast": [
                {
                    "region": REGION,
                    "zone": ZONE,
                    "value": 6
                }
            ]
        }
    }
    quotas1 = {
        "quotas": {
    "compute_cores":[
        {"region": "ru-1", "used": 0, "zone": "ru-1b", "value": 10}
    ],
        "compute_ram":[
                      {"region": "ru-1", "used": 0, "zone": "ru-1b", "value": 262144}
            ],
        "image_gigabytes":[{"region": "ru-1", "used": 0, "zone": None, "value": 5000}],
        "volume_gigabytes_basic":[{"region": "ru-1", "used": 0, "zone": "ru-1b", "value": 5000}],
        "volume_gigabytes_fast":[{"region": "ru-1", "used": 0, "zone": "ru-1b", "value": 5000}],
        "volume_gigabytes_local":[{"region": "ru-1", "used": 0, "zone": "ru-1b", "value": 0} ],
        "volume_gigabytes_universal":[{"region": "ru-1", "used": 0, "zone": "ru-1b", "value": 5000}]
    }
    }
    quotas1 = {
        "quotas": {
            "compute_cores": [
                {
                    "region": REGION,
                    "zone": ZONE,
                    "value": 1
                }
            ],
            "compute_ram": [
                {
                    "region": REGION,
                    "zone": ZONE,
                    "value": 512
                }
            ],
            "volume_gigabytes_fast": [
                {
                    "region": REGION,
                    "zone": ZONE,
                    "value": 5
                }
            ]
        }
    }
    '''
    ##project1=selvpc.projects.create('TestAnna')
    ##project=selvpc.projects.list()[0]
    q = selvpc.quotas.get_project_quotas('bc71e8181b4a497e9c18c365df3e605a')
    project = selvpc.projects.list()[0]
    Quatas = project.update_quotas(Qoates)
Ejemplo n.º 26
0
def RoleManager():
    http_client = setup_http_client(
        api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    role = selvpc.users.roles_manager
    print(role)
Ejemplo n.º 27
0
# You can create and get api token here
# https://support.selectel.ru/keys
#
VPC_TOKEN = os.getenv('SEL_TOKEN', None)

#
# You can get actual api URL here
# https://support.selectel.ru/vpc/docs
#
VPC_URL = "https://api.selectel.ru/vpc/resell"

REGION = "ru-2"
ZONE = "ru-2a"

http_client = setup_http_client(api_url=VPC_URL, api_token=VPC_TOKEN)
selvpc = Client(client=http_client)

project = selvpc.projects.create(name="Awesome Projectq12")
logging.info("Project '%s' has been created with id '%s'", project.name,
             project.id)

quotas = {
    "quotas": {
        "compute_cores": [{
            "region": REGION,
            "zone": ZONE,
            "value": 1
        }],
        "compute_ram": [{
            "region": REGION,
            "zone": ZONE,
Ejemplo n.º 28
0
def CreateUser(user):
    http_client = setup_http_client(
        api_url=VPC_URL, api_token='G7MeHkggaaQDV6DP8ZQx8S3Dp_82142')
    selvpc = Client(client=http_client)
    user = selvpc.users.create(user['name'], user['password'], return_raw=True)