Example #1
0
    def _generate_tm_profile(self, name, routing, endpoints, record):
        # set appropriate endpoint types
        endpoint_type_prefix = 'Microsoft.Network/trafficManagerProfiles/'
        for ep in endpoints:
            if ep.target_resource_id:
                ep.type = endpoint_type_prefix + 'nestedEndpoints'
            elif ep.target:
                ep.type = endpoint_type_prefix + 'externalEndpoints'
            else:
                msg = ('Invalid endpoint {} in profile {}, needs to have ' +
                       'either target or target_resource_id').format(
                           ep.name, name)
                raise AzureException(msg)

        # build and return
        return Profile(
            id=self._profile_name_to_id(name),
            name=name,
            traffic_routing_method=routing,
            dns_config=DnsConfig(
                relative_name=name,
                ttl=record.ttl,
            ),
            monitor_config=_get_monitor(record),
            endpoints=endpoints,
            location='global',
        )
 def _create_or_update_traffic_manager(
     self,
     tag: str,
     traffic_manager_name: str,
     appgw_public_ip_addresses: typing.List[PublicIPAddress],
 ) -> Profile:
     """
     Creates new or updates existing Traffic Manager in Azure.
     """
     logging.debug("Creating or updating traffic manager")
     tm_profile = self.traffic_manager.profiles.create_or_update(
         resource_group_name=self.options.azure_trafficmanager_resource_group,
         profile_name=traffic_manager_name,
         parameters=Profile(
             name=traffic_manager_name,
             location="global",
             tags=get_policy_tags(additional_tags={TAG_APPGWGROUP: tag}),
             profile_status="Enabled",
             traffic_routing_method="Priority",
             dns_config=DnsConfig(
                 relative_name=traffic_manager_name, ttl=TRAFFICMANAGER_DNS_TTL
             ),
             monitor_config=MonitorConfig(
                 protocol="https",
                 port=80,
                 path="/",
                 interval_in_seconds=10,
                 timeout_in_seconds=5,
                 tolerated_number_of_failures=1,
                 custom_headers=[
                     MonitorConfigCustomHeadersItem(
                         name="Host", value=TRAFFICMANAGER_HEALTHCHECK_HOSTNAME
                     )
                 ],
                 expected_status_code_ranges=[
                     MonitorConfigExpectedStatusCodeRangesItem(min=200, max=399)
                 ],
             ),
             endpoints=[],
         ),
     )
     # Construct endpoints for traffic manager
     for index, ip in enumerate(appgw_public_ip_addresses):
         self.traffic_manager.endpoints.create_or_update(
             resource_group_name=self.options.azure_trafficmanager_resource_group,
             profile_name=tm_profile.name,
             endpoint_type="AzureEndpoints",
             endpoint_name=f"endpoint-{index}",
             parameters=Endpoint(
                 name=f"endpoint-{index}",
                 target_resource_id=ip.id,
                 target=ip.ip_address,
             ),
         )
     return tm_profile
Example #3
0
def create_dns_config_instance(dns_config):
    return DnsConfig(relative_name=dns_config['relative_name'],
                     ttl=dns_config['ttl'])
    def create_or_update_traffic_manager_profile(self, update):
        '''
        Create or update a Traffic Manager profile.
        :param name: name of a traffic  manager
        :return: traffic manage object
        '''
        self.log('Creating or updating Traffic Manager {0}'.format(self.name))
        try:
            # Create MonitorConfig
            monitor_config = MonitorConfig(
                protocol=self.monitor_config.get('protocol', 'HTTP'),
                port=self.monitor_config.get('port', 80),
                path=self.monitor_config.get('path', '/'),
                timeout_in_seconds=self.monitor_config.get(
                    'timeout_in_seconds', 10),
                interval_in_seconds=self.monitor_config.get(
                    'interval_in_seconds', 30),
                tolerated_number_of_failures=self.monitor_config.get(
                    'tolerated_number_of_failures', 3))
            # Create DnsConfig
            dns_config = DnsConfig(relative_name=self.dns_config.get(
                'relative_name', self.name),
                                   ttl=self.dns_config.get('ttl', 60))

            # Create Endpoints
            endpoints = []
            for end in self.endpoints:
                endpoint_instance = Endpoint(
                    name=end.get('name'),
                    type=end.get('type'),
                    target=end.get('target'),
                    endpoint_status=end.get('endpoint_status', 'Enabled'),
                    weight=end.get('weight'),
                    priority=end.get('priority'),
                    target_resource_id=end.get('target_resource_id'),
                    endpoint_location=end.get('endpoint_location'),
                    min_child_endpoints=end.get('min_child_endpoints'),
                    geo_mapping=end.get('geo_mapping'))
                endpoints.append(endpoint_instance)

            profile = Profile(
                tags=self.tags,
                location="global",
                profile_status=self.profile_status,
                traffic_routing_method=self.traffic_routing_method,
                dns_config=dns_config,
                monitor_config=monitor_config,
                endpoints=endpoints)
            return self.trafficmanager_client.profiles.create_or_update(
                self.resource_group, self.name, profile).as_dict()
        except CloudError as cloud_error:
            if update:
                self.fail(
                    'Error Updating the Traffic Manager: {0}. {1}'.format(
                        self.name, str(cloud_error)))
            else:
                self.fail(
                    'Error Creating the Traffic Manager: {0}. {1}'.format(
                        self.name, str(cloud_error)))
        except Exception as exc:
            self.fail('Error retrieving Traffic Manager {0} - {1}'.format(
                self.name, str(exc)))