예제 #1
0
  def testLoadYaml(self):
    self.Touch(
        '.', 'basic.yaml', """\
name: wpname
project_id: argo-workerpool-test
worker_count: 2
regions: [us-central1]
worker_config:
  network:
    project_id: argo-workerpool-test
    network: argo-workerpool-test-network
    subnetwork: argo-workerpool-test-subnet
""")
    wp = workerpool_config.LoadWorkerpoolConfigFromPath('basic.yaml',
                                                        self.messages)
    self.assertEqual(
        wp,
        self.messages.WorkerPool(
            name='wpname',
            projectId='argo-workerpool-test',
            workerCount=2,
            regions=[self._regions.us_central1],
            workerConfig=self.messages.WorkerConfig(
                network=self.messages.Network(
                    projectId='argo-workerpool-test',
                    network='argo-workerpool-test-network',
                    subnetwork='argo-workerpool-test-subnet'))))
예제 #2
0
  def testLoadJson(self):
    self.Touch(
        '.', 'basic.json', """\
{
  "name": "wpname",
  "project_id": "argo-workerpool-test",
  "worker_count": 2,
  "regions": ["us-central1"],
  "worker_config": {
    "network": {
      "project_id": "argo-workerpool-test",
      "network": "argo-workerpool-test-network",
      "subnetwork": "argo-workerpool-test-subnet",
    },
  },
}
""")
    wp = workerpool_config.LoadWorkerpoolConfigFromPath('basic.json',
                                                        self.messages)
    self.assertEqual(
        wp,
        self.messages.WorkerPool(
            name='wpname',
            projectId='argo-workerpool-test',
            workerCount=2,
            regions=[self._regions.us_central1],
            workerConfig=self.messages.WorkerConfig(
                network=self.messages.Network(
                    projectId='argo-workerpool-test',
                    network='argo-workerpool-test-network',
                    subnetwork='argo-workerpool-test-subnet'))))
예제 #3
0
  def testYamlSyntaxError(self):
    """Misplaced brace at the end of the document."""
    self.Touch(
        '.', 'error.yaml', """\
name: wpname
project_id: argo-workerpool-test
worker_count: 2
regions: us-central1
worker_config:
  network:
    project_id: argo-workerpool-test
    network: argo-workerpool-test-network
    subnetwork: argo-workerpool-test-subnet
}
""")
    with self.assertRaisesRegex(cloudbuild_util.ParserError, 'error.yaml'):
      workerpool_config.LoadWorkerpoolConfigFromPath('error.yaml',
                                                     self.messages)
예제 #4
0
  def testYamlUnusedField(self):
    """testYamlUnusedField checks a misindented field."""
    self.Touch(
        '.', 'error.yaml', """\
name: wpname
project_id: argo-workerpool-test
worker_count: 2
regions: ["us-central1"]
worker_config:
network:
  project_id: argo-workerpool-test
  network: argo-workerpool-test-network
  subnetwork: argo-workerpool-test-subnet
""")
    with self.assertRaisesRegex(
        cloudbuild_util.ParseProtoException,
        r'error.yaml as workerpool config: .network: unused'):
      workerpool_config.LoadWorkerpoolConfigFromPath('error.yaml',
                                                     self.messages)
예제 #5
0
  def testJsonUnusedField(self):
    """testJsonUnusedField checks a misplaced field."""
    self.Touch(
        '.', 'error.json', """\
{
  "name": "wpname",
  "project_id": "argo-workerpool-test",
  "worker_count": 2,
  "regions": ["us-central1"],
  "network": {
    "project_id": "argo-workerpool-test",
    "network": "argo-workerpool-test-network",
    "subnetwork": "argo-workerpool-test-subnet",
  },
}
""")
    with self.assertRaisesRegex(
        cloudbuild_util.ParseProtoException,
        r'error.json as workerpool config: .network: unused'):
      workerpool_config.LoadWorkerpoolConfigFromPath('error.json',
                                                     self.messages)
예제 #6
0
  def testYamlUnusedNested(self):
    """Only present an error for the highest-level mistake."""
    self.Touch(
        '.', 'error.yaml', """\
name: wpname
project_id: argo-workerpool-test
worker_count: 2
regions: us-central1
worker_config:
  network:
    project_id: argo-workerpool-test
    network: argo-workerpool-test-network
    subnetwork: argo-workerpool-test-subnet
extra:
  data:
    is: "bad"
""")
    with self.assertRaisesRegex(
        cloudbuild_util.ParseProtoException,
        r'error\.yaml as workerpool config: \.extra: unused'):
      workerpool_config.LoadWorkerpoolConfigFromPath('error.yaml',
                                                     self.messages)
예제 #7
0
  def testYamlMultipleUnused(self):
    """More than one mistake on the same level gets a more interesting error."""
    self.Touch(
        '.', 'error.yaml', """\
name: wpname
project_id: argo-workerpool-test
worker_count: 2
regions: ["us-central1"]
worker_config:
  network:
    project_id: argo-workerpool-test
    network: argo-workerpool-test-network
    subnetwork: argo-workerpool-test-subnet
extra:
  data:
    is: "bad"
nonsense: "bad as well"
""")
    with self.assertRaisesRegex(
        cloudbuild_util.ParseProtoException,
        r'error\.yaml as workerpool config: \.\{extra,nonsense\}: unused'):
      workerpool_config.LoadWorkerpoolConfigFromPath('error.yaml',
                                                     self.messages)
예제 #8
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """

        wp_region = args.region

        release_track = self.ReleaseTrack()
        client = cloudbuild_util.GetClientInstance(release_track,
                                                   region=wp_region)
        messages = cloudbuild_util.GetMessagesModule(release_track)

        # Get the workerpool proto from either the flags or the specified file.
        wp = messages.WorkerPool()
        if args.config_from_file is not None:
            wp = workerpool_config.LoadWorkerpoolConfigFromPath(
                args.config_from_file, messages)
        else:
            wp.name = args.WORKER_POOL
            if args.peered_network is not None:
                network_config = messages.NetworkConfig()
                network_config.peeredNetwork = args.peered_network
                wp.networkConfig = network_config
            worker_config = messages.WorkerConfig()
            if args.worker_machine_type is not None:
                worker_config.machineType = args.worker_machine_type
            if args.worker_disk_size is not None:
                worker_config.diskSizeGb = compute_utils.BytesToGb(
                    args.worker_disk_size)
            wp.workerConfig = worker_config

        parent = properties.VALUES.core.project.Get(required=True)

        # Get the parent project.location ref
        parent_resource = resources.REGISTRY.Create(
            collection='cloudbuild.projects.locations',
            projectsId=parent,
            locationsId=wp_region)

        # Send the Create request
        created_op = client.projects_locations_workerPools.Create(
            messages.CloudbuildProjectsLocationsWorkerPoolsCreateRequest(
                workerPool=wp,
                parent=parent_resource.RelativeName(),
                workerPoolId=wp.name))

        raw_dict = encoding.MessageToDict(created_op.response)
        created_wp = encoding.DictToMessage(raw_dict, messages.WorkerPool)

        # Get the workerpool ref
        wp_resource = resources.REGISTRY.Parse(
            None,
            collection='cloudbuild.projects.locations.workerPools',
            api_version=cloudbuild_util.
            RELEASE_TRACK_TO_API_VERSION[release_track],
            params={
                'projectsId': parent,
                'locationsId': wp_region,
                'workerPoolsId': created_wp.name,
            })

        log.CreatedResource(wp_resource)

        # Format the workerpool name for display
        try:
            created_wp.name = cloudbuild_util.RegionalWorkerPoolShortName(
                created_wp.name)
        except ValueError:
            pass  # Must be an old version.

        return created_wp
예제 #9
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """

        client = cloudbuild_util.GetClientInstanceAlpha()
        messages = cloudbuild_util.GetMessagesModuleAlpha()

        parent = properties.VALUES.core.project.Get(required=True)

        # Get the workerpool proto from either the flags or the specified file.
        wp = messages.WorkerPool()
        if args.config_from_file is not None:
            wp = workerpool_config.LoadWorkerpoolConfigFromPath(
                args.config_from_file, messages)
        else:
            wp.name = args.WORKER_POOL
            if args.worker_count is not None:
                try:
                    wp.workerCount = int(args.worker_count)
                except ValueError as e:
                    raise c_exceptions.InvalidArgumentException(
                        '--worker-count', e)
            if args.remove_regions is not None:
                regions_to_remove = set()
                for region_str in args.remove_regions:
                    region = Update._region_choice_to_enum[region_str]
                    regions_to_remove.add(region)
                new_regions = []
                for region in wp.regions:
                    if region not in regions_to_remove:
                        new_regions.append(region)
                wp.regions[:] = new_regions
            if args.clear_regions:
                wp.regions[:] = []
            if args.add_regions is not None:
                for region_str in args.add_regions:
                    region = Update._region_choice_to_enum[region_str]
                    wp.regions.append(region)
            worker_config = messages.WorkerConfig()
            if args.worker_machine_type is not None:
                worker_config.machineType = args.worker_machine_type
            if args.worker_disk_size is not None:
                worker_config.diskSizeGb = compute_utils.BytesToGb(
                    args.worker_disk_size)
            if any([
                    args.worker_network_project is not None,
                    args.worker_network_name is not None,
                    args.worker_network_subnet is not None
            ]):
                if not all([
                        args.worker_network_project is not None,
                        args.worker_network_name is not None,
                        args.worker_network_subnet is not None
                ]):
                    raise c_exceptions.RequiredArgumentException(
                        '--worker_network_*',
                        'The flags --worker_network_project, --worker_network_name, and '
                        '--worker_network_subnet must all be set if any of them are set.'
                    )
                # At this point all network flags are set, but not necessarily truthy
                network = messages.Network()
                if args.worker_network_project:
                    network.projectId = args.worker_network_project
                else:
                    network.projectId = parent
                if args.worker_network_name:
                    network.network = args.worker_network_name
                else:
                    network.network = 'default'
                if args.worker_network_subnet:
                    network.subnetwork = args.worker_network_subnet
                else:
                    network.subnetwork = 'default'
                worker_config.network = network
            if args.worker_tag is not None:
                worker_config.tag = args.worker_tag
            wp.workerConfig = worker_config

        # Get the workerpool ref
        wp_resource = resources.REGISTRY.Parse(
            None,
            collection='cloudbuild.projects.workerPools',
            api_version='v1alpha1',
            params={
                'projectsId': parent,
                'workerPoolsId': wp.name,
            })

        # Send the Update request
        updated_wp = client.projects_workerPools.Patch(
            messages.CloudbuildProjectsWorkerPoolsPatchRequest(
                name=wp_resource.RelativeName(), workerPool=wp))

        log.UpdatedResource(wp_resource)

        return updated_wp
예제 #10
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """

        wp_region = args.region

        release_track = self.ReleaseTrack()
        client = cloudbuild_util.GetClientInstance(release_track)
        messages = cloudbuild_util.GetMessagesModule(release_track)

        parent = properties.VALUES.core.project.Get(required=True)

        # Get the workerpool proto from either the flags or the specified file.
        wp = messages.WorkerPool()
        if args.config_from_file is not None:
            wp = workerpool_config.LoadWorkerpoolConfigFromPath(
                args.config_from_file, messages)
        else:
            wp.name = args.WORKER_POOL
            if args.peered_network is not None:
                network_config = messages.NetworkConfig()
                network_config.peeredNetwork = args.peered_network
                wp.networkConfig = network_config
            worker_config = messages.WorkerConfig()
            if args.worker_machine_type is not None:
                worker_config.machineType = args.worker_machine_type
            if args.worker_disk_size is not None:
                worker_config.diskSizeGb = compute_utils.BytesToGb(
                    args.worker_disk_size)
            if args.no_external_ip:
                worker_config.noExternalIp = True
            wp.workerConfig = worker_config

        # Get the workerpool ref
        wp_resource = resources.REGISTRY.Parse(
            None,
            collection='cloudbuild.projects.locations.workerPools',
            api_version=cloudbuild_util.
            RELEASE_TRACK_TO_API_VERSION[release_track],
            params={
                'projectsId': parent,
                'locationsId': wp_region,
                'workerPoolsId': wp.name,
            })

        update_mask = cloudbuild_util.MessageToFieldPaths(wp)
        req = messages.CloudbuildProjectsLocationsWorkerPoolsPatchRequest(
            name=wp_resource.RelativeName(),
            workerPool=wp,
            updateMask=','.join(update_mask))
        # Send the Update request
        updated_op = client.projects_locations_workerPools.Patch(req)

        op_resource = resources.REGISTRY.ParseRelativeName(
            updated_op.name,
            collection='cloudbuild.projects.locations.operations')
        updated_wp = waiter.WaitFor(
            waiter.CloudOperationPoller(client.projects_locations_workerPools,
                                        client.projects_locations_operations),
            op_resource, 'Updating worker pool')

        log.UpdatedResource(wp_resource)

        # Format the workerpool name for display
        try:
            updated_wp.name = cloudbuild_util.RegionalWorkerPoolShortName(
                updated_wp.name)
        except ValueError:
            pass  # Must be an old version.

        return updated_wp
예제 #11
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """

        wp_name = args.WORKER_POOL
        wp_region = args.region

        release_track = self.ReleaseTrack()
        client = cloudbuild_util.GetClientInstance(release_track)
        messages = cloudbuild_util.GetMessagesModule(release_track)

        # Get the workerpool proto from either the flags or the specified file.
        wp = messages.WorkerPool()
        if args.config_from_file is not None:
            try:
                wp = workerpool_config.LoadWorkerpoolConfigFromPath(
                    args.config_from_file, messages)
                # Don't allow a worker pool config for hybrid pools in any other
                # track but alpha.
                if release_track != base.ReleaseTrack.ALPHA:
                    if wp.hybridPoolConfig is not None:
                        raise cloudbuild_exceptions.HybridNonAlphaConfigError
            except cloudbuild_exceptions.ParseProtoException as err:
                log.err.Print(
                    '\nFailed to parse configuration from file. If you'
                    ' were a Private Preview user, note that the format for this'
                    ' file has changed slightly for GA.\n')
                raise err
        else:
            wp.privatePoolV1Config = messages.PrivatePoolV1Config()

            network_config = messages.NetworkConfig()
            if args.peered_network is not None:
                network_config.peeredNetwork = args.peered_network
            # All of the egress flags are mutually exclusive with each other.
            if args.no_public_egress or (release_track == base.ReleaseTrack.GA
                                         and args.no_external_ip):
                network_config.egressOption = messages.NetworkConfig.EgressOptionValueValuesEnum.NO_PUBLIC_EGRESS
            wp.privatePoolV1Config.networkConfig = network_config

            worker_config = messages.WorkerConfig()
            if args.worker_machine_type is not None:
                worker_config.machineType = args.worker_machine_type
            if args.worker_disk_size is not None:
                worker_config.diskSizeGb = compute_utils.BytesToGb(
                    args.worker_disk_size)
            wp.privatePoolV1Config.workerConfig = worker_config

        parent = properties.VALUES.core.project.Get(required=True)

        # Get the parent project.location ref
        parent_resource = resources.REGISTRY.Create(
            collection='cloudbuild.projects.locations',
            projectsId=parent,
            locationsId=wp_region)

        # Send the Create request
        created_op = client.projects_locations_workerPools.Create(
            messages.CloudbuildProjectsLocationsWorkerPoolsCreateRequest(
                workerPool=wp,
                parent=parent_resource.RelativeName(),
                workerPoolId=wp_name))

        op_resource = resources.REGISTRY.ParseRelativeName(
            created_op.name,
            collection='cloudbuild.projects.locations.operations')
        created_wp = waiter.WaitFor(
            waiter.CloudOperationPoller(client.projects_locations_workerPools,
                                        client.projects_locations_operations),
            op_resource, 'Creating worker pool')

        # Get the workerpool ref
        wp_resource = resources.REGISTRY.Parse(
            None,
            collection='cloudbuild.projects.locations.workerPools',
            api_version=cloudbuild_util.
            RELEASE_TRACK_TO_API_VERSION[release_track],
            params={
                'projectsId': parent,
                'locationsId': wp_region,
                'workerPoolsId': wp_name,
            })

        log.CreatedResource(wp_resource)

        return created_wp
예제 #12
0
 def testBadEncoding(self):
   self.Touch('.', 'garbage.garbage', """this file is neither json nor yaml""")
   with self.assertRaisesRegex(cloudbuild_util.ParserError,
                               'Could not parse as a dictionary'):
     workerpool_config.LoadWorkerpoolConfigFromPath('garbage.garbage',
                                                    self.messages)
예제 #13
0
 def testNoFile(self):
   with self.assertRaises(files.MissingFileError):
     workerpool_config.LoadWorkerpoolConfigFromPath('not-here.json',
                                                    self.messages)
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    """

    wp_name = args.WORKER_POOL
    wp_region = args.region

    release_track = self.ReleaseTrack()
    client = cloudbuild_util.GetClientInstance(release_track)
    messages = cloudbuild_util.GetMessagesModule(release_track)

    parent = properties.VALUES.core.project.Get(required=True)

    # Get the workerpool proto from either the flags or the specified file.
    wp = messages.WorkerPool()
    if args.config_from_file is not None:
      try:
        wp = workerpool_config.LoadWorkerpoolConfigFromPath(
            args.config_from_file, messages)
      except cloudbuild_exceptions.ParseProtoException as err:
        log.err.Print('\nFailed to parse configuration from file. If you'
                      ' were a Beta user, note that the format for this'
                      ' file has changed slightly for GA.\n')
        raise err
    else:
      private_worker_config = messages.WorkerConfig()
      if args.worker_machine_type is not None:
        private_worker_config.machineType = args.worker_machine_type
      if args.worker_disk_size is not None:
        private_worker_config.diskSizeGb = compute_utils.BytesToGb(
            args.worker_disk_size)

      private_worker_network_config = messages.NetworkConfig()
      # All of the egress flags are mutually exclusive with each other.
      if args.no_public_egress or (release_track == base.ReleaseTrack.GA and
                                   args.no_external_ip):
        private_worker_network_config.egressOption = messages.NetworkConfig.EgressOptionValueValuesEnum.NO_PUBLIC_EGRESS
      if args.public_egress:
        private_worker_network_config.egressOption = messages.NetworkConfig.EgressOptionValueValuesEnum.PUBLIC_EGRESS

      # The private pool and hybrid pool flags are mutually exclusive
      hybrid_worker_config = messages.HybridWorkerConfig()
      if args.default_build_disk_size is not None:
        hybrid_worker_config.diskSizeGb = compute_utils.BytesToGb(
            args.default_build_disk_size)
      if args.default_build_memory is not None:
        hybrid_worker_config.memoryGb = compute_utils.BytesToGb(
            args.default_build_memory)
      if args.default_build_vcpu_count is not None:
        hybrid_worker_config.vcpuCount = args.default_build_vcpu_count

      if args.default_build_disk_size is not None or args.default_build_memory is not None or args.default_build_vcpu_count is not None:
        wp.hybridPoolConfig = messages.HybridPoolConfig()
        wp.hybridPoolConfig.defaultWorkerConfig = hybrid_worker_config
      else:
        wp.privatePoolV1Config = messages.PrivatePoolV1Config()
        wp.privatePoolV1Config.networkConfig = private_worker_network_config
        wp.privatePoolV1Config.workerConfig = private_worker_config

    # Get the workerpool ref
    wp_resource = resources.REGISTRY.Parse(
        None,
        collection='cloudbuild.projects.locations.workerPools',
        api_version=cloudbuild_util.RELEASE_TRACK_TO_API_VERSION[release_track],
        params={
            'projectsId': parent,
            'locationsId': wp_region,
            'workerPoolsId': wp_name,
        })

    update_mask = cloudbuild_util.MessageToFieldPaths(wp)
    req = messages.CloudbuildProjectsLocationsWorkerPoolsPatchRequest(
        name=wp_resource.RelativeName(),
        workerPool=wp,
        updateMask=','.join(update_mask))
    # Send the Update request
    updated_op = client.projects_locations_workerPools.Patch(req)

    op_resource = resources.REGISTRY.ParseRelativeName(
        updated_op.name, collection='cloudbuild.projects.locations.operations')
    updated_wp = waiter.WaitFor(
        waiter.CloudOperationPoller(client.projects_locations_workerPools,
                                    client.projects_locations_operations),
        op_resource, 'Updating worker pool')

    log.UpdatedResource(wp_resource)

    return updated_wp