Example #1
0
    def Run(self, args):
        """Create a domain mapping."""
        conn_context = connection_context.GetConnectionContext(
            args, product=flags.Product.RUN)
        domain_mapping_ref = args.CONCEPTS.domain.Parse()

        # Check if the provided domain has already been verified
        # if mapping to a non-CRoGKE service
        if flags.GetPlatform() == flags.PLATFORM_MANAGED:
            client = global_methods.GetServerlessClientInstance()
            all_domains = global_methods.ListVerifiedDomains(
                client, flags.GetRegion(args))
            # If not already verified, explain and error out
            if all(d.id not in domain_mapping_ref.Name() for d in all_domains):
                if not all_domains:
                    domains_text = 'You currently have no verified domains.'
                else:
                    domains = ['* {}'.format(d.id) for d in all_domains]
                    domains_text = ('Currently verified domains:\n{}'.format(
                        '\n'.join(domains)))
                raise exceptions.DomainMappingCreationError(
                    'The provided domain does not appear to be verified '
                    'for the current account so a domain mapping '
                    'cannot be created. Visit [{help}] for more information.'
                    '\n{domains}'.format(help=DOMAIN_MAPPINGS_HELP_DOCS_URL,
                                         domains=domains_text))

        with serverless_operations.Connect(conn_context) as client:
            mapping = client.CreateDomainMapping(domain_mapping_ref,
                                                 args.service,
                                                 args.force_override)
            for record in mapping.records:
                record.name = record.name or mapping.route_name
            return mapping.records
 def Run(self, args):
     try:
         project = properties.VALUES.core.project.Get()
         client = api_client.GetApiClientForTrack(self.ReleaseTrack())
         return client.ListVerifiedDomains()
     # Note: the domain user-verified listing API is availible through two
     # routes, one App Engine and one Cloud Run. The command should work if the
     # user has *either* API activated. The following falls back to Cloud Run
     # if the user does not have App Engine activated.
     except (apitools_exceptions.HttpNotFoundError,
             apitools_exceptions.HttpForbiddenError) as appengine_err:
         try:
             run_client = run_methods.GetServerlessClientInstance()
             return run_methods.ListVerifiedDomains(run_client)
         except (apitools_exceptions.HttpNotFoundError,
                 apitools_exceptions.HttpForbiddenError):
             log.error(
                 'To list user-verified domains, you must activate either'
                 ' the App Engine or Cloud Run API and have read permissions '
                 'on one of them.')
             log.error('To activate App Engine, visit:')
             log.error(
                 'https://console.cloud.google.com/apis/api/'
                 'appengine.googleapis.com/overview?project={}'.format(
                     project))
             log.error('To activate Cloud Run, visit:')
             log.error(
                 'https://console.cloud.google.com/apis/api/'
                 'run.googleapis.com/overview?project={}'.format(project))
             raise appengine_err
Example #3
0
    def Run(self, args):
        """Create a domain mapping."""
        # domains.cloudrun.com api group only supports v1alpha1 on clusters.
        conn_context = connection_context.GetConnectionContext(
            args,
            flags.Product.RUN,
            self.ReleaseTrack(),
            version_override=('v1alpha1' if platforms.GetPlatform() !=
                              platforms.PLATFORM_MANAGED else None))
        domain_mapping_ref = args.CONCEPTS.domain.Parse()
        changes = [
            config_changes.SetLaunchStageAnnotationChange(self.ReleaseTrack())
        ]

        # Check if the provided domain has already been verified
        # if mapping to a non-CRoGKE service
        if platforms.GetPlatform() == platforms.PLATFORM_MANAGED:
            client = global_methods.GetServerlessClientInstance()
            all_domains = global_methods.ListVerifiedDomains(client)
            # If not already verified, explain and error out
            if all(d.id not in domain_mapping_ref.Name() for d in all_domains):
                if not all_domains:
                    domains_text = 'You currently have no verified domains.'
                else:
                    domains = ['* {}'.format(d.id) for d in all_domains]
                    domains_text = ('Currently verified domains:\n{}'.format(
                        '\n'.join(domains)))
                raise exceptions.DomainMappingCreationError(
                    'The provided domain does not appear to be verified '
                    'for the current account so a domain mapping '
                    'cannot be created. Visit [{help}] for more information.'
                    '\n{domains}'.format(help=DOMAIN_MAPPINGS_HELP_DOCS_URL,
                                         domains=domains_text))

        with serverless_operations.Connect(conn_context) as client:
            try:
                mapping = client.CreateDomainMapping(domain_mapping_ref,
                                                     args.service, changes,
                                                     args.force_override)
            except exceptions.DomainMappingAlreadyExistsError as e:
                if console_io.CanPrompt() and console_io.PromptContinue(
                    ('This domain is already being used as a mapping elsewhere. '
                     'The existing mapping can be overriden by passing '
                     '`--force-override` or by continuing at the prompt below.'
                     ),
                        prompt_string='Override the existing mapping'):
                    deletion.Delete(domain_mapping_ref,
                                    client.GetDomainMapping,
                                    client.DeleteDomainMapping,
                                    async_=False)
                    mapping = client.CreateDomainMapping(
                        domain_mapping_ref, args.service, changes, True)
                else:
                    raise e

            for record in mapping.records:
                record.name = record.name or mapping.route_name
            return mapping.records