def __init__(self, module):
        """
        Constructor
        """
        super(VmwareTagManager, self).__init__(module)
        self.pyv = PyVmomi(module=module)

        self.object_type = self.params.get("object_type")
        self.object_name = self.params.get("object_name")
        self.managed_object = None

        if self.object_type == "VirtualMachine":
            self.managed_object = self.pyv.get_vm_or_template(self.object_name)

        if self.object_type == "Datacenter":
            self.managed_object = self.pyv.find_datacenter_by_name(
                self.object_name)

        if self.object_type == "ClusterComputeResource":
            self.managed_object = self.pyv.find_cluster_by_name(
                self.object_name)

        if self.object_type == "HostSystem":
            self.managed_object = self.pyv.find_hostsystem_by_name(
                self.object_name)

        if self.object_type == "DistributedVirtualSwitch":
            self.managed_object = find_dvs_by_name(self.pyv.content,
                                                   self.object_name)
            self.object_type = "VmwareDistributedVirtualSwitch"

        if self.object_type == "DistributedVirtualPortgroup":
            dvs_name, pg_name = self.object_name.split(":", 1)
            dv_switch = find_dvs_by_name(self.pyv.content, dvs_name)
            if dv_switch is None:
                self.module.fail_json(
                    msg=
                    "A distributed virtual switch with name %s does not exist"
                    % dvs_name)
            self.managed_object = find_dvspg_by_name(dv_switch, pg_name)

        if self.managed_object is None:
            self.module.fail_json(
                msg="Failed to find the managed object for %s with type %s" %
                (self.object_name, self.object_type))

        if not hasattr(self.managed_object, "_moId"):
            self.module.fail_json(
                msg="Unable to find managed object id for %s managed object" %
                self.object_name)

        self.dynamic_managed_object = DynamicID(type=self.object_type,
                                                id=self.managed_object._moId)

        self.tag_service = self.api_client.tagging.Tag
        self.category_service = self.api_client.tagging.Category
        self.tag_association_svc = self.api_client.tagging.TagAssociation

        self.tag_names = self.params.get("tag_names")
    def state_migrate_vss_vds(self):
        host_network_system = self.host_system.configManager.networkSystem

        dv_switch = find_dvs_by_name(self.content, self.migrate_switch_name)
        pg = find_dvspg_by_name(dv_switch, self.migrate_portgroup_name)

        config = vim.host.NetworkConfig()
        config.portgroup = [self.create_port_group_config()]
        config.vnic = [self.create_host_vnic_config(dv_switch.uuid, pg.key)]
        host_network_system.UpdateNetworkConfig(config, "modify")
        self.module.exit_json(changed=True)
Ejemplo n.º 3
0
    def __init__(self, module):
        super(PyVmomiHelper, self).__init__(module)
        if self.params['network']:
            self.network_type = self.params['network'].get('type')
            self.ip_address = self.params['network'].get('ip_address', None)
            self.subnet_mask = self.params['network'].get('subnet_mask', None)
            self.default_gateway = self.params['network'].get(
                'default_gateway', None)
            self.tcpip_stack = self.params['network'].get('tcpip_stack')
        self.device = self.params['device']
        if self.network_type == 'dhcp' and not self.device:
            module.fail_json(
                msg=
                "device is a required parameter when network type is set to 'dhcp'"
            )
        self.mtu = self.params['mtu']
        self.enable_vsan = self.params['enable_vsan']
        self.enable_vmotion = self.params['enable_vmotion']
        self.enable_mgmt = self.params['enable_mgmt']
        self.enable_ft = self.params['enable_ft']
        self.enable_provisioning = self.params['enable_provisioning']
        self.enable_replication = self.params['enable_replication']
        self.enable_replication_nfc = self.params['enable_replication_nfc']

        self.vswitch_name = self.params['vswitch_name']
        self.vds_name = self.params['dvswitch_name']
        self.port_group_name = self.params['portgroup_name']

        self.esxi_host_name = self.params['esxi_hostname']
        hosts = self.get_all_host_objs(esxi_host_name=self.esxi_host_name)
        if hosts:
            self.esxi_host_obj = hosts[0]
        else:
            self.module.fail_json(
                msg=
                "Failed to get details of ESXi server. Please specify esxi_hostname."
            )

        if self.network_type == 'static':
            if self.module.params['state'] == 'absent':
                pass
            elif not self.ip_address:
                module.fail_json(
                    msg=
                    "ip_address is a required parameter when network type is set to 'static'"
                )
            elif not self.subnet_mask:
                module.fail_json(
                    msg=
                    "subnet_mask is a required parameter when network type is set to 'static'"
                )

        # find Port Group
        if self.vswitch_name:
            self.port_group_obj = self.get_port_group_by_name(
                host_system=self.esxi_host_obj,
                portgroup_name=self.port_group_name,
                vswitch_name=self.vswitch_name)
            if not self.port_group_obj:
                module.fail_json(msg="Portgroup '%s' not found on vSS '%s'" %
                                 (self.port_group_name, self.vswitch_name))
        elif self.vds_name:
            self.dv_switch_obj = find_dvs_by_name(self.content, self.vds_name)
            if not self.dv_switch_obj:
                module.fail_json(msg="vDS '%s' not found" % self.vds_name)
            self.port_group_obj = find_dvspg_by_name(self.dv_switch_obj,
                                                     self.port_group_name)
            if not self.port_group_obj:
                module.fail_json(msg="Portgroup '%s' not found on vDS '%s'" %
                                 (self.port_group_name, self.vds_name))

        # find VMkernel Adapter
        if self.device:
            self.vnic = self.get_vmkernel_by_device(device_name=self.device)
        else:
            # config change (e.g. DHCP to static, or vice versa); doesn't work with virtual port change
            self.vnic = self.get_vmkernel_by_portgroup_new(
                port_group_name=self.port_group_name)
            if not self.vnic and self.network_type == 'static':
                # vDS to vSS or vSS to vSS (static IP)
                self.vnic = self.get_vmkernel_by_ip(ip_address=self.ip_address)
    def check_dvspg_state(self):
        self.dv_switch = find_dvs_by_name(self.content,
                                          self.module.params['switch_name'])

        if self.dv_switch is None:
            self.module.fail_json(
                msg="A distributed virtual switch with name %s does not exist"
                % self.module.params['switch_name'])
        self.dvs_portgroup = find_dvspg_by_name(
            self.dv_switch, self.module.params['portgroup_name'])

        if self.dvs_portgroup is None:
            return 'absent'

        # Check config
        # Basic config
        if self.dvs_portgroup.config.numPorts != self.module.params[
                'num_ports']:
            return 'update'

        # Default port config
        defaultPortConfig = self.dvs_portgroup.config.defaultPortConfig
        if self.module.params['vlan_trunk']:
            if not isinstance(
                    defaultPortConfig.vlan,
                    vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec):
                return 'update'
            if map(lambda x: (x.start, x.end),
                   defaultPortConfig.vlan.vlanId) != self.create_vlan_list():
                return 'update'
        else:
            if not isinstance(
                    defaultPortConfig.vlan,
                    vim.dvs.VmwareDistributedVirtualSwitch.VlanIdSpec):
                return 'update'
            if defaultPortConfig.vlan.vlanId != int(
                    self.module.params['vlan_id']):
                return 'update'

        if defaultPortConfig.securityPolicy.allowPromiscuous.value != self.module.params['network_policy']['promiscuous'] or \
                defaultPortConfig.securityPolicy.forgedTransmits.value != self.module.params['network_policy']['forged_transmits'] or \
                defaultPortConfig.securityPolicy.macChanges.value != self.module.params['network_policy']['mac_changes']:
            return 'update'

        # Teaming Policy
        teamingPolicy = self.dvs_portgroup.config.defaultPortConfig.uplinkTeamingPolicy
        if teamingPolicy.policy.value != self.module.params['teaming_policy']['load_balance_policy'] or \
                teamingPolicy.reversePolicy.value != self.module.params['teaming_policy']['inbound_policy'] or \
                teamingPolicy.notifySwitches.value != self.module.params['teaming_policy']['notify_switches'] or \
                teamingPolicy.rollingOrder.value != self.module.params['teaming_policy']['rolling_order']:
            return 'update'

        # PG policy (advanced_policy)
        policy = self.dvs_portgroup.config.policy
        if policy.blockOverrideAllowed != self.module.params['port_policy']['block_override'] or \
                policy.ipfixOverrideAllowed != self.module.params['port_policy']['ipfix_override'] or \
                policy.livePortMovingAllowed != self.module.params['port_policy']['live_port_move'] or \
                policy.networkResourcePoolOverrideAllowed != self.module.params['port_policy']['network_rp_override'] or \
                policy.portConfigResetAtDisconnect != self.module.params['port_policy']['port_config_reset_at_disconnect'] or \
                policy.securityPolicyOverrideAllowed != self.module.params['port_policy']['security_override'] or \
                policy.shapingOverrideAllowed != self.module.params['port_policy']['shaping_override'] or \
                policy.trafficFilterOverrideAllowed != self.module.params['port_policy']['traffic_filter_override'] or \
                policy.uplinkTeamingOverrideAllowed != self.module.params['port_policy']['uplink_teaming_override'] or \
                policy.vendorConfigOverrideAllowed != self.module.params['port_policy']['vendor_config_override'] or \
                policy.vlanOverrideAllowed != self.module.params['port_policy']['vlan_override']:
            return 'update'

        # PG Type
        if self.dvs_portgroup.config.type != self.module.params[
                'portgroup_type']:
            return 'update'

        return 'present'