Ejemplo n.º 1
0
    def Run(self, args):
        dataproc = dp.Dataproc(self.ReleaseTrack())

        cluster_ref = args.CONCEPTS.cluster.Parse()

        request = dataproc.messages.DataprocProjectsRegionsClustersGetRequest(
            projectId=cluster_ref.projectId,
            region=cluster_ref.region,
            clusterName=cluster_ref.clusterName)

        cluster = dataproc.client.projects_regions_clusters.Get(request)

        # Filter out Dataproc-generated labels.
        clusters.DeleteGeneratedLabels(cluster, dataproc)

        schema_path = self.GetSchemaPath()
        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=cluster,
                                   stream=stream,
                                   schema_path=schema_path)
        else:
            export_util.Export(message=cluster,
                               stream=sys.stdout,
                               schema_path=schema_path)
Ejemplo n.º 2
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        # Retrieve the specified compute instance.
        instance_ref = flags.INSTANCE_ARG.ResolveAsResource(
            args,
            holder.resources,
            scope_lister=flags.GetInstanceZoneScopeLister(client))

        request = client.messages.ComputeInstancesGetRequest(
            **instance_ref.AsDict())
        instance = client.MakeRequests([(client.apitools_client.instances,
                                         'Get', request)])[0]

        # Get JSON Schema for Compute Engine instances (located in
        # third_party/py/googlecloudsdk/schemas/...).
        schema_path = self.GetSchemaPath(for_help=False)

        # Write configuration data to either designated file or stdout.
        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=instance,
                                   stream=stream,
                                   schema_path=schema_path)
            return log.status.Print('Exported [{}] to \'{}\'.'.format(
                instance.name, args.destination))
        else:
            export_util.Export(message=instance,
                               stream=sys.stdout,
                               schema_path=schema_path)
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        (backend_services_utils.
         IsDefaultRegionalBackendServicePropertyNoneWarnOtherwise())
        backend_service_ref = (
            flags.GLOBAL_REGIONAL_BACKEND_SERVICE_ARG.ResolveAsResource(
                args,
                holder.resources,
                scope_lister=compute_flags.GetDefaultScopeLister(client)))

        get_request = backend_services_utils.ComposeGetRequest(
            client, backend_service_ref)

        backend_service = client.MakeRequests([get_request])[0]

        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=backend_service,
                                   stream=stream,
                                   schema_path=self.GetSchemaPath())
        else:
            export_util.Export(message=backend_service,
                               stream=sys.stdout,
                               schema_path=self.GetSchemaPath())
  def Run(self, args):
    dataproc = dp.Dataproc(self.ReleaseTrack())

    template_ref = args.CONCEPTS.template.Parse()

    # Get specified version, or most recent version if no version arg provided.
    workflow_template = dataproc.GetRegionsWorkflowTemplate(
        template_ref, args.version)

    # Filter out OUTPUT_ONLY fields and resource identifying fields. Note this
    # needs to be kept in sync with v1 workflow_templates.proto.
    workflow_template.id = None
    workflow_template.name = None
    workflow_template.version = None
    workflow_template.createTime = None
    workflow_template.updateTime = None
    # We do not need to clear any fields from workflow_template.placement.
    # 1) Managed cluster:
    #    a) cluster_name is really a name prefix, so it's okay that multiple
    #       templates have the same value.
    #    b) The server does not resolve OUTPUT_ONLY fields when storing a
    #       workflow template, so cluster_config is fine as is.
    # 2) Cluster selector: there are no OUTPUT_ONLY or directly resource
    # identifying fields here.

    if args.destination:
      with files.FileWriter(args.destination) as stream:
        export_util.Export(message=workflow_template, stream=stream)
    else:
      export_util.Export(message=workflow_template, stream=sys.stdout)
Ejemplo n.º 5
0
    def Run(self, args):
        dataproc = dp.Dataproc(self.ReleaseTrack())
        messages = dataproc.messages

        policy_ref = args.CONCEPTS.autoscaling_policy.Parse()

        request = messages.DataprocProjectsRegionsAutoscalingPoliciesGetRequest(
            name=policy_ref.RelativeName())
        policy = dataproc.client.projects_regions_autoscalingPolicies.Get(
            request)

        schema_path = export_util.GetSchemaPath('dataproc',
                                                dataproc.api_version,
                                                'AutoscalingPolicy',
                                                for_help=False)
        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=policy,
                                   stream=stream,
                                   schema_path=schema_path)
        else:
            # Print to stdout
            export_util.Export(message=policy,
                               stream=sys.stdout,
                               schema_path=schema_path)
Ejemplo n.º 6
0
    def testImportAutoscalingPolicies_update_declineOverwrite(self):
        policy = self.MakeAutoscalingPolicy('fake-project',
                                            'antarctica-north42', 'policy-1')

        # Write to a file so that we can use stdin to decline the prompt. Otherwise,
        # we wouldn't have a way to demarcate where the policy ends and declining
        # the prompt starts.
        file_name = os.path.join(self.temp_path, 'policy.yaml')
        with files.FileWriter(file_name) as stream:
            export_util.Export(
                message=policy,
                stream=stream,
            )

        expected_request_policy = copy.deepcopy(policy)
        expected_request_policy.name = None

        self.mock_client.projects_regions_autoscalingPolicies.Create.Expect(
            self.messages.
            DataprocProjectsRegionsAutoscalingPoliciesCreateRequest(
                parent='projects/fake-project/regions/antarctica-north42',
                autoscalingPolicy=expected_request_policy,
            ),
            exception=self.MakeHttpError(status_code=409))

        # Don't pass --quiet, and decline the prompt
        self.WriteInput('n\n')
        with self.AssertRaisesExceptionMatches(
                console_io.OperationCancelledError, 'Aborted by user.'):
            self.RunDataproc(
                'autoscaling-policies import policy-1 --source {}'.format(
                    file_name))
Ejemplo n.º 7
0
    def _testImportAutoscalingPolicies_create(self,
                                              region=None,
                                              region_flag=''):
        if region is None:
            region = self.REGION

        policy = self.MakeAutoscalingPolicy('fake-project', region, 'policy-1')

        self.WriteInput(export_util.Export(message=policy))

        expected_request_policy = copy.deepcopy(policy)
        expected_request_policy.name = None

        response_policy = copy.deepcopy(expected_request_policy)
        response_policy.name = ('projects/fake-project/regions/{0}/'
                                'autoscalingPolicies/policy-1'.format(region))

        self.mock_client.projects_regions_autoscalingPolicies.Create.Expect(
            self.messages.
            DataprocProjectsRegionsAutoscalingPoliciesCreateRequest(
                parent='projects/fake-project/regions/{0}'.format(region),
                autoscalingPolicy=expected_request_policy,
            ),
            response=response_policy)

        result = self.RunDataproc(
            'autoscaling-policies import policy-1 {0}'.format(region_flag))
        self.AssertMessagesEqual(response_policy, result)
Ejemplo n.º 8
0
    def testImportFromFile(self):
        target_grpc_proxy = copy.deepcopy(self._existing_target_grpc_proxy)
        target_grpc_proxy.description = 'changed'
        self.make_requests.side_effect = iter(
            [[test_resources.TARGET_GRPC_PROXIES_ALPHA[0]],
             [target_grpc_proxy]])

        # Write the modified target_grpc_proxies to a file
        file_name = os.path.join(self.temp_path, 'temp-tgp.yaml')
        with files.FileWriter(file_name) as stream:
            export_util.Export(message=target_grpc_proxy, stream=stream)
        self.WriteInput('y\n')

        response = self.RunImport('{0} --source {1}'.format(
            self._resource_name, file_name))
        self.CheckRequests([
            (self._target_grpc_proxies_api, 'Get',
             self.messages.ComputeTargetGrpcProxiesGetRequest(
                 project='my-project', targetGrpcProxy='target-grpc-proxy-1'))
        ], [(self._target_grpc_proxies_api, 'Patch',
             self.messages.ComputeTargetGrpcProxiesPatchRequest(
                 project='my-project',
                 targetGrpcProxy=self._resource_name,
                 targetGrpcProxyResource=target_grpc_proxy))])
        self.assertEqual(response, target_grpc_proxy)
Ejemplo n.º 9
0
    def _testImportWorkflowTemplatesFromStdIn(self,
                                              region=None,
                                              region_flag=''):
        if region is None:
            region = self.REGION

        # Provided template does not have an id or a name.
        provided_template = self.MakeWorkflowTemplate(region=region)
        provided_template.id = None
        provided_template.name = None

        # The id is populated before we make the create request.
        expected_request = copy.deepcopy(provided_template)
        expected_request.id = self.WORKFLOW_TEMPLATE

        # The create response has the name populated.
        expected_response = copy.deepcopy(expected_request)
        expected_response.name = self.WorkflowTemplateName(region=region)

        self.WriteInput(export_util.Export(provided_template))
        self.ExpectGetWorkflowTemplate(
            exception=self.MakeHttpError(status_code=404), region=region)
        self.ExpectCreateWorkflowTemplate(expected_request,
                                          expected_response,
                                          region=region)
        result = self.RunDataproc('workflow-templates import {0} {1}'.format(
            self.WORKFLOW_TEMPLATE, region_flag))
        self.AssertMessagesEqual(expected_response, result)
Ejemplo n.º 10
0
    def testImportWorkflowTemplatesUpdateExisting(self):
        # Provided template does not have an id or a name.
        provided_template = self.MakeWorkflowTemplate()
        provided_template.id = None
        provided_template.name = None

        get_response = self.MakeWorkflowTemplate()
        get_response.version = 1

        # The id, name, and version are populated before we make the update request.
        expected_request = copy.deepcopy(provided_template)
        expected_request.id = self.WORKFLOW_TEMPLATE
        expected_request.name = self.WorkflowTemplateName()
        expected_request.version = 1

        # Response has version incremented.
        expected_response = copy.deepcopy(expected_request)
        expected_response.version = 2

        # Write test template to file.
        file_name = os.path.join(self.temp_path, 'template.yaml')
        with files.FileWriter(file_name) as stream:
            export_util.Export(message=provided_template, stream=stream)

        self.ExpectGetWorkflowTemplate(response=get_response)
        self.ExpectUpdateWorkflowTemplate(workflow_template=expected_request,
                                          response=expected_response)
        self.WriteInput('y\n')
        result = self.RunDataproc(
            'workflow-templates import {0} --source {1}'.format(
                self.WORKFLOW_TEMPLATE, file_name))
        self.AssertMessagesEqual(expected_response, result)
Ejemplo n.º 11
0
    def testImportWorkflowTemplatesCreateNewWithRegion(self):
        # Set region property.
        properties.VALUES.dataproc.region.Set('us-test1')

        # Provided template does not have an id or a name.
        provided_template = self.MakeWorkflowTemplate()
        provided_template.id = None
        provided_template.name = None

        # The id is populated before we make the create request.
        expected_request = copy.deepcopy(provided_template)
        expected_request.id = self.WORKFLOW_TEMPLATE

        # The create response has the name populated.
        expected_response = copy.deepcopy(expected_request)
        expected_response.name = self.WorkflowTemplateName(region='us-test1')

        # Write test template to file.
        file_name = os.path.join(self.temp_path, 'template.yaml')
        with files.FileWriter(file_name) as stream:
            export_util.Export(message=provided_template, stream=stream)

        self.ExpectGetWorkflowTemplate(
            name=self.WorkflowTemplateName(region='us-test1'),
            exception=self.MakeHttpError(status_code=404))
        parent = self.WorkflowTemplateParentName(region='us-test1')
        self.ExpectCreateWorkflowTemplate(workflow_template=expected_request,
                                          response=expected_response,
                                          parent=parent)
        result = self.RunDataproc(
            'workflow-templates import {0} --source {1}'.format(
                self.WORKFLOW_TEMPLATE, file_name))
        self.AssertMessagesEqual(expected_response, result)
Ejemplo n.º 12
0
    def Run(self, args):
        dataproc = dp.Dataproc(self.ReleaseTrack())

        template_ref = args.CONCEPTS.template.Parse()

        # Get specified version, or most recent version if no version arg provided.
        workflow_template = dataproc.GetRegionsWorkflowTemplate(
            template_ref, args.version)

        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=workflow_template,
                                   stream=stream,
                                   schema_path=self.GetSchemaPath())
        else:
            export_util.Export(message=workflow_template,
                               stream=sys.stdout,
                               schema_path=self.GetSchemaPath())
Ejemplo n.º 13
0
 def testPoliciesImport_update(self):
   with self.CreatedPolicy(self.autoscaling_policy_id):
     new_request = self.MakeImportableAutoscalingPolicy()
     new_request.basicAlgorithm.cooldownPeriod = '3600s'
     self.WriteInput(export_util.Export(message=new_request))
     updated = self.RunDataproc(
         'autoscaling-policies import --quiet {0}'.format(
             self.autoscaling_policy_id))
     self.assertEqual(self.autoscaling_policy_id, updated.id)
     self.assertEqual('3600s', updated.basicAlgorithm.cooldownPeriod)
Ejemplo n.º 14
0
  def CreatedPolicy(self, policy_id):
    policy = self.MakeImportableAutoscalingPolicy()
    self.WriteInput(export_util.Export(message=policy))

    try:
      yield self.RunDataproc('autoscaling-policies import {0}'.format(
          policy_id))
    finally:
      self.RunDataproc('autoscaling-policies delete {0} --quiet'.format(
          policy_id))
Ejemplo n.º 15
0
 def testUpdateFromStdIn(self):
     """Tests updating an instance from stdin."""
     self.WriteInput(export_util.Export(self._modified_instance))
     self._RunUpdate('instance-1 --zone=zone-1 --project=my-project')
     self.CheckRequests([(self.compute.instances, 'Update',
                          self.messages.ComputeInstancesUpdateRequest(
                              instance='instance-1',
                              zone='zone-1',
                              project='my-project',
                              instanceResource=self._modified_instance))])
Ejemplo n.º 16
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        forwarding_rule_ref = self.FORWARDING_RULE_ARG.ResolveAsResource(
            args,
            holder.resources,
            scope_lister=compute_flags.GetDefaultScopeLister(client))

        forwarding_rule = utils.SendGetRequest(client, forwarding_rule_ref)

        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=forwarding_rule,
                                   stream=stream,
                                   schema_path=self.GetSchemaPath())
        else:
            export_util.Export(message=forwarding_rule,
                               stream=sys.stdout,
                               schema_path=self.GetSchemaPath())
Ejemplo n.º 17
0
    def testImportAutoscalingPolicies_create_withoutRegionProperty(self):
        policy = self.MakeAutoscalingPolicy('fake-project', 'foobar',
                                            'policy-1')

        self.WriteInput(export_util.Export(message=policy))

        # No region is specified via flag or config.
        regex = r'Failed to find attribute \[region\]'
        with self.assertRaisesRegex(handlers.ParseError, regex):
            self.RunDataproc('autoscaling-policies import policy-1',
                             set_region=False)
Ejemplo n.º 18
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())

    target_grpc_proxy_ref = self.TARGET_GRPC_PROXY_ARG.ResolveAsResource(
        args, holder.resources, default_scope=compute_scope.ScopeEnum.GLOBAL)

    target_grpc_proxy = _Describe(holder, target_grpc_proxy_ref)

    if args.destination:
      with files.FileWriter(args.destination) as stream:
        export_util.Export(
            message=target_grpc_proxy,
            stream=stream,
            schema_path=self.GetSchemaPath())

    else:
      export_util.Export(
          message=target_grpc_proxy,
          stream=sys.stdout,
          schema_path=self.GetSchemaPath())
Ejemplo n.º 19
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client

    url_map_ref = self.URL_MAP_ARG.ResolveAsResource(
        args,
        holder.resources,
        scope_lister=compute_flags.GetDefaultScopeLister(client))

    url_map = url_maps_utils.SendGetRequest(client, url_map_ref)

    if args.destination:
      with files.FileWriter(args.destination) as stream:
        export_util.Export(message=url_map,
                           stream=stream,
                           schema_path=self.GetSchemaPath())
    else:
      export_util.Export(message=url_map,
                         stream=sys.stdout,
                         schema_path=self.GetSchemaPath())
Ejemplo n.º 20
0
    def testExportClustersToStdOut(self):
        cluster = self.MakeCluster()

        # Expected output has cluster-specific info cleared.
        expected_output = copy.deepcopy(cluster)
        expected_output.clusterName = None
        expected_output.projectId = None

        self.ExpectGetCluster(cluster)
        self.RunDataproc('clusters export {0}'.format(self.CLUSTER_NAME))
        self.AssertOutputEquals(export_util.Export(expected_output))
Ejemplo n.º 21
0
    def testImportBackendServiceFromStdIn(self):
        backend_service_ref = self.GetBackendServiceRef('my-backend-service')
        backend_service = copy.deepcopy(self._backend_services[0])

        self.ExpectGetRequest(backend_service_ref=backend_service_ref,
                              exception=http_error.MakeHttpError(code=404))
        self.ExpectInsertRequest(backend_service_ref=backend_service_ref,
                                 backend_service=backend_service)

        self.WriteInput(export_util.Export(backend_service))

        self.RunImport('my-backend-service --global')
Ejemplo n.º 22
0
def _Run(args, holder, url_map_arg, release_track):
    """Issues requests necessary to export URL maps."""
    client = holder.client

    url_map_ref = url_map_arg.ResolveAsResource(
        args,
        holder.resources,
        scope_lister=compute_flags.GetDefaultScopeLister(client))

    url_map = url_maps_utils.SendGetRequest(client, url_map_ref)

    codecs.RegisterL7TrafficControlCodecs(_GetApiVersion(release_track))
    if args.destination:
        with files.FileWriter(args.destination) as stream:
            export_util.Export(message=url_map,
                               stream=stream,
                               schema_path=_GetSchemaPath(release_track))
    else:
        export_util.Export(message=url_map,
                           stream=sys.stdout,
                           schema_path=_GetSchemaPath(release_track))
Ejemplo n.º 23
0
  def testImportFromStdInRegion(self):
    forwarding_rule_ref = self.GetForwardingRuleRef('fr-1', region='alaska')
    forwarding_rule = copy.deepcopy(self._forwarding_rules[0])

    self.WriteInput(export_util.Export(forwarding_rule))

    self.ExpectGetRequest(forwarding_rule_ref=forwarding_rule_ref,
                          exception=http_error.MakeHttpError(code=404))
    self.ExpectInsertRequest(forwarding_rule_ref,
                             forwarding_rule=forwarding_rule)

    self.RunImport('fr-1 --region alaska')
Ejemplo n.º 24
0
    def Run(self, args):
        dataproc = dp.Dataproc(self.ReleaseTrack())
        messages = dataproc.messages

        policy_ref = args.CONCEPTS.autoscaling_policy.Parse()

        request = messages.DataprocProjectsRegionsAutoscalingPoliciesGetRequest(
            name=policy_ref.RelativeName())
        policy = dataproc.client.projects_regions_autoscalingPolicies.Get(
            request)

        # Filter out OUTPUT_ONLY fields and resource identifying fields. Note this
        # needs to be kept in sync with v1 autoscaling_policies.proto.
        policy.id = None
        policy.name = None

        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=policy, stream=stream)
        else:
            # Print to stdout
            export_util.Export(message=policy, stream=sys.stdout)
Ejemplo n.º 25
0
def _Run(args, holder, target_http_proxy_arg, release_track):
    """Issues requests necessary to export target HTTP proxies."""
    client = holder.client

    target_http_proxy_ref = target_http_proxy_arg.ResolveAsResource(
        args,
        holder.resources,
        default_scope=compute_scope.ScopeEnum.GLOBAL,
        scope_lister=compute_flags.GetDefaultScopeLister(client))

    target_http_proxy = target_http_proxies_utils.SendGetRequest(
        client, target_http_proxy_ref)

    if args.destination:
        with files.FileWriter(args.destination) as stream:
            export_util.Export(message=target_http_proxy,
                               stream=stream,
                               schema_path=_GetSchemaPath(release_track))
    else:
        export_util.Export(message=target_http_proxy,
                           stream=sys.stdout,
                           schema_path=_GetSchemaPath(release_track))
    def Run(self, args):
        dataproc = dp.Dataproc(self.ReleaseTrack())

        cluster_ref = args.CONCEPTS.cluster.Parse()

        request = dataproc.messages.DataprocProjectsRegionsClustersGetRequest(
            projectId=cluster_ref.projectId,
            region=cluster_ref.region,
            clusterName=cluster_ref.clusterName)

        cluster = dataproc.client.projects_regions_clusters.Get(request)

        # Filter out Dataproc-generated labels and properties.
        clusters.DeleteGeneratedLabels(cluster, dataproc)
        clusters.DeleteGeneratedProperties(cluster, dataproc)

        RemoveNonImportableFields(cluster)
        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=cluster, stream=stream)
        else:
            export_util.Export(message=cluster, stream=sys.stdout)
Ejemplo n.º 27
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        helper = ssl_policies_utils.SslPolicyHelper(holder)
        client = holder.client

        ssl_policy_ref = self.SSL_POLICY_ARG.ResolveAsResource(
            args,
            holder.resources,
            scope_lister=compute_flags.GetDefaultScopeLister(client),
            default_scope=compute_scope.ScopeEnum.GLOBAL)

        ssl_policy = helper.Describe(ssl_policy_ref)

        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=ssl_policy,
                                   stream=stream,
                                   schema_path=self.GetSchemaPath())
        else:
            export_util.Export(message=ssl_policy,
                               stream=sys.stdout,
                               schema_path=self.GetSchemaPath())
Ejemplo n.º 28
0
    def testReadAutoscalingPolicy_stdin(self):
        policy = self.MakeAutoscalingPolicy('cool-project', 'cool-region',
                                            'cool-policy')

        self.WriteInput(export_util.Export(message=policy))

        expected_policy = copy.deepcopy(policy)
        expected_policy.name = None

        policy_read = util.ReadAutoscalingPolicy(
            dataproc=self.dataproc_mock,
            policy_id='cool-policy',
        )
        self.AssertMessagesEqual(expected_policy, policy_read)
Ejemplo n.º 29
0
    def testExportWorkflowTemplatesWithVersion(self):
        workflow_template = self.MakeWorkflowTemplate(version=2,
                                                      labels={'foo': 'bar'})

        # Expected output has template-specific info cleared.
        expected_output = copy.deepcopy(workflow_template)
        expected_output.id = None
        expected_output.name = None
        expected_output.version = None

        self.ExpectGetWorkflowTemplate(version=2, response=workflow_template)
        self.RunDataproc('workflow-templates export {0} --version 2'.format(
            self.WORKFLOW_TEMPLATE))
        self.AssertOutputEquals(export_util.Export(expected_output))
 def testInstantiateFromFileWorkflowTemplatesAsync(self):
     workflow_template = self.MakeWorkflowTemplate()
     workflow_template.id = None
     workflow_template.name = None
     file_name = os.path.join(self.temp_path, 'template.yaml')
     with files.FileWriter(file_name) as stream:
         export_util.Export(message=workflow_template, stream=stream)
     self.ExpectWorkflowTemplatesInstantiateInline(
         workflow_template=workflow_template)
     self.RunDataproc(
         'workflow-templates instantiate-from-file --file {0} --async'.
         format(file_name))
     self.AssertOutputEquals('')
     self.AssertErrContains('Instantiating with operation [{0}].'.format(
         self.OperationName()))