Ejemplo n.º 1
0
def _get_monitor(record):
    monitor = MonitorConfig(
        protocol=record.healthcheck_protocol,
        port=record.healthcheck_port,
        path=record.healthcheck_path,
    )
    host = record.healthcheck_host
    if host:
        monitor.custom_headers = [MonitorConfigCustomHeadersItem(
            name='Host', value=host
        )]
    return monitor
Ejemplo n.º 2
0
def create_monitor_config_instance(monitor_config):
    return MonitorConfig(
        profile_monitor_status=monitor_config['profile_monitor_status'],
        protocol=monitor_config['protocol'],
        port=monitor_config['port'],
        path=monitor_config['path'],
        interval_in_seconds=monitor_config['interval'],
        timeout_in_seconds=monitor_config['timeout'],
        tolerated_number_of_failures=monitor_config['tolerated_failures'])
 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
    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)))