コード例 #1
0
def listEdgeClusters(module, stub_config):
    ec_list = []
    try:
        ec_svc = EdgeClusters(stub_config)
        ec_list = ec_svc.list()
    except Error as ex:
        api_error = ex.date.convert_to(ApiError)
        module.fail_json(msg='API Error listing Edge Clusters: %s'%(api_error.error_message))
    return ec_list
コード例 #2
0
def main():
    module = AnsibleModule(argument_spec=dict(
        display_name=dict(required=True, type='str'),
        description=dict(required=False, type='str', default=None),
        members=dict(required=False, type='list', default=None),
        tags=dict(required=False, type='dict', default=None),
        state=dict(required=False,
                   type='str',
                   default="present",
                   choices=['present', 'absent']),
        nsx_manager=dict(required=True, type='str'),
        nsx_username=dict(required=True, type='str'),
        nsx_passwd=dict(required=True, type='str', no_log=True)),
                           supports_check_mode=True)

    if not HAS_PYNSXT:
        module.fail_json(msg='pynsxt is required for this module')
    session = requests.session()
    session.verify = False
    nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443)
    connector = connect.get_requests_connector(session=session,
                                               msg_protocol='rest',
                                               url=nsx_url)
    stub_config = StubConfigurationFactory.new_std_configuration(connector)
    security_context = create_user_password_security_context(
        module.params["nsx_username"], module.params["nsx_passwd"])
    connector.set_security_context(security_context)
    requests.packages.urllib3.disable_warnings()
    tags = None
    if module.params['tags'] is not None:
        tags = []
        for key, value in module.params['tags'].items():
            tag = Tag(scope=key, tag=value)
            tags.append(tag)

    member_list = []
    if module.params['members']:
        for tnode_name in module.params['members']:
            member = getTransportNodeByName(tnode_name, module, stub_config)
            edgeClusterMember = EdgeClusterMember(transport_node_id=member.id)
            member_list.append(edgeClusterMember)
    elif module.params['members'] is None:
        member_list = None
    ec_svc = EdgeClusters(stub_config)
    ec = getEdgeClusterByName(module, stub_config)
    if module.params['state'] == 'present':
        if ec is None:
            new_ec = EdgeCluster(display_name=module.params['display_name'],
                                 description=module.params['description'],
                                 members=member_list,
                                 tags=tags)
            if module.check_mode:
                module.exit_json(changed=True,
                                 debug_out=str(new_ec),
                                 id="1111")
            new_ec = ec_svc.create(new_ec)
            module.exit_json(changed=True,
                             object_name=module.params['display_name'],
                             id=new_ec.id,
                             message="Edge Cluster with name %s created!" %
                             (module.params['display_name']))
        elif ec:
            changed = False
            if tags != ec.tags:
                ec.tags = tags
                changed = True
            desiredList = []
            realisedList = []
            if module.params['members']:
                desiredList = simplifyClusterMembersList(member_list)
            if ec.members:
                realisedList = simplifyClusterMembersList(ec.members)
            if desiredList != realisedList:
                ec.members = member_list
                changed = True
            if changed:
                if module.check_mode:
                    module.exit_json(changed=True, debug_out=str(ec), id=ec.id)
                new_ec = ec_svc.update(ec.id, ec)
                module.exit_json(
                    changed=True,
                    object_name=module.params['display_name'],
                    id=new_ec.id,
                    message="Edge Cluster with name %s has changed tags!" %
                    (module.params['display_name']))

            module.exit_json(
                changed=False,
                object_name=module.params['display_name'],
                id=ec.id,
                message="Edge Cluster with name %s already exists!" %
                (module.params['display_name']))

    elif module.params['state'] == "absent":
        if ec:
            if module.check_mode:
                module.exit_json(changed=True, debug_out=str(ec))

            ec_svc.delete(ec.id)
            module.exit_json(changed=True,
                             object_name=module.params['display_name'],
                             message="Edge Cluster with name %s deleted!" %
                             (module.params['display_name']))
        if module.check_mode:
            module.exit_json(changed=False,
                             debug_out="no Edge Cluster with name %s" %
                             (module.params['display_name']))

        module.exit_json(changed=False,
                         object_name=module.params['display_name'],
                         message="Edge Cluster with name %s does not exist!" %
                         (module.params['display_name']))
コード例 #3
0
def main():
    module = AnsibleModule(argument_spec=dict(
        display_name=dict(required=True, type='str'),
        description=dict(required=False, type='str', default=None),
        edge_cluster_member=dict(required=True, type='str'),
        ip_address=dict(required=True, type='str'),
        t0_router=dict(required=True, type='str'),
        logical_switch_port_id=dict(required=True, type='str'),
        urpf=dict(required=False,
                  type='str',
                  default='NONE',
                  choices=['NONE', 'STRICT']),
        tags=dict(required=False, type='dict', default=None),
        state=dict(required=False,
                   type='str',
                   default="present",
                   choices=['present', 'absent']),
        nsx_manager=dict(required=True, type='str'),
        nsx_username=dict(required=True, type='str'),
        nsx_passwd=dict(required=True, type='str', no_log=True)),
                           supports_check_mode=True)

    if not HAS_PYNSXT:
        module.fail_json(msg='pynsxt is required for this module')
    session = requests.session()
    session.verify = False
    nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443)
    connector = connect.get_requests_connector(session=session,
                                               msg_protocol='rest',
                                               url=nsx_url)
    stub_config = StubConfigurationFactory.new_std_configuration(connector)
    security_context = create_user_password_security_context(
        module.params["nsx_username"], module.params["nsx_passwd"])
    connector.set_security_context(security_context)
    requests.packages.urllib3.disable_warnings()
    tags = None
    if module.params['tags'] is not None:
        tags = []
        for key, value in module.params['tags'].items():
            tag = Tag(scope=key, tag=value)
            tags.append(tag)
    lr_svc = LogicalRouters(stub_config)
    lr = getLogicalRouterByName(module, stub_config)
    ec_svc = EdgeClusters(stub_config)
    edgeCluster = ec_svc.get(lr.edge_cluster_id)

    tn = getTransportNodeByName(module, stub_config)
    member_index = []
    for member in edgeCluster.members:
        if member.transport_node_id == tn.id:
            member_index.append(int(member.member_index))

    subnet_list = []
    subnet = module.params['ip_address'].split('/')
    new_subnet = IPSubnet(ip_addresses=[subnet[0]],
                          prefix_length=int(subnet[1]))
    subnet_list.append(new_subnet)
    lrp_svc = LogicalRouterPorts(stub_config)
    lrp = getLogicalRouterPortByName(module, stub_config, lr.id)
    if module.params['state'] == 'present':
        if lrp is None:
            new_lrp = LogicalRouterUpLinkPort(
                display_name=module.params['display_name'],
                description=module.params['description'],
                edge_cluster_member_index=member_index,
                linked_logical_switch_port_id=ResourceReference(
                    target_id=module.params['logical_switch_port_id']),
                subnets=subnet_list,
                urpf_mode=module.params['urpf'],
                logical_router_id=lr.id,
                tags=tags)
            if module.check_mode:
                module.exit_json(changed=True,
                                 debug_out=str(new_lrp),
                                 id="1111")
            try:
                lrp_temp = lrp_svc.create(new_lrp)
                new_lrp = lrp_temp.convert_to(LogicalRouterUpLinkPort)
                module.exit_json(
                    changed=True,
                    object_name=module.params['display_name'],
                    id=new_lrp.id,
                    message="Logical Router Port with name %s created!" %
                    (module.params['display_name']))
            except Error as ex:
                api_error = ex.data.convert_to(ApiError)
                module.fail_json(
                    msg='API Error creating Logical Router Uplink: %s' %
                    (str(api_error.error_message)))
        elif lrp:
            changed = False
            if lrp.linked_logical_switch_port_id.target_id != module.params[
                    'logical_switch_port_id']:
                changed = True
                lrp.linked_logical_switch_port_id = ResourceReference(
                    target_id=module.params['logical_switch_port_id'])
            if tags != lrp.tags:
                changed = True
                lrp.tags = tags
            if subnet_list != lrp.subnets:
                changed = True
                lrp.subnets = subnet_list
            if member_index != lrp.edge_cluster_member_index:
                changed = True
                lrp.edge_cluster_member_index = member_index
            if changed:
                if module.check_mode:
                    module.exit_json(changed=True,
                                     debug_out=str(lrp),
                                     id=lrp.id)
                new_lr = lrp_svc.update(lrp.id, lrp)
                module.exit_json(
                    changed=True,
                    object_name=module.params['display_name'],
                    id=lrp.id,
                    message=
                    "Logical Router Uplink with name %s has been changed!" %
                    (module.params['display_name']))
            module.exit_json(
                changed=False,
                object_name=module.params['display_name'],
                id=lrp.id,
                message="Logical Router Uplink with name %s already exists!" %
                (module.params['display_name']))

    elif module.params['state'] == "absent":

        if lrp:
            if module.check_mode:
                module.exit_json(changed=True, debug_out=str(lrp), id=lrp.id)
            try:
                lrp_svc.delete(lrp.id, force=True)
            except Error as ex:
                api_error = ex.date.convert_to(ApiError)
                module.fail_json(
                    msg='API Error deleting Logical Router Ports: %s' %
                    (api_error.error_message))

            module.exit_json(
                changed=True,
                object_name=module.params['display_name'],
                message="Logical Router Port with name %s deleted!" %
                (module.params['display_name']))
        module.exit_json(
            changed=False,
            object_name=module.params['display_name'],
            message="Logical Router Port with name %s does not exist!" %
            (module.params['display_name']))