Example #1
0
 def ListConfigurations(self, namespace_ref):
   messages = self._messages_module
   request = messages.ServerlessNamespacesConfigurationsListRequest(
       parent=namespace_ref.RelativeName())
   with metrics.record_duration(metrics.LIST_CONFIGURATIONS):
     response = self._client.namespaces_configurations.List(request)
   return [service.Service(item, messages) for item in response.items]
Example #2
0
 def ListRoutes(self, namespace_ref):
   messages = self._messages_module
   request = messages.ServerlessNamespacesRoutesListRequest(
       parent=namespace_ref.RelativeName())
   with metrics.record_duration(metrics.LIST_ROUTES):
     response = self._client.namespaces_routes.List(request)
   return [service.Service(item, messages) for item in response.items]
Example #3
0
  def GetService(self, service_ref):
    """Return the relevant Service from the server, or None if 404."""
    messages = self._messages_module
    service_get_request = messages.ServerlessNamespacesServicesGetRequest(
        name=service_ref.RelativeName())

    try:
      with metrics.record_duration(metrics.GET_SERVICE):
        service_get_response = self._client.namespaces_services.Get(
            service_get_request)
      return service.Service(service_get_response, messages)
    except api_exceptions.HttpNotFoundError:
      return None
Example #4
0
  def _UpdateOrCreateService(self, service_ref,
                             config_changes):
    """Apply config_changes to the service. Create it if necessary."""
    messages = self._messages_module
    # GET the Service
    serv = self.GetService(service_ref)
    if serv:
      # PUT the changed Service
      for config_change in config_changes:
        config_change.AdjustConfiguration(serv.configuration, serv.metadata)
      serv_name = service_ref.RelativeName()
      serv_update_req = (
          messages.ServerlessNamespacesServicesReplaceServiceRequest(
              service=serv.Message(),
              name=serv_name))
      with metrics.record_duration(metrics.UPDATE_SERVICE):
        updated = self._client.namespaces_services.ReplaceService(
            serv_update_req)
      return service.Service(updated, messages)

    else:
      # POST a new Service
      new_serv = service.Service.New(self._client, service_ref.namespacesId)
      new_serv.name = service_ref.servicesId
      pretty_print.Info('Creating new service [{bold}{service}{reset}]',
                        service=new_serv.name)
      parent = service_ref.Parent().RelativeName()
      for config_change in config_changes:
        config_change.AdjustConfiguration(new_serv.configuration,
                                          new_serv.metadata)
      serv_create_req = (
          messages.ServerlessNamespacesServicesCreateRequest(
              service=new_serv.Message(),
              parent=parent))
      with metrics.record_duration(metrics.CREATE_SERVICE):
        raw_service = self._client.namespaces_services.Create(
            serv_create_req)
      return service.Service(raw_service, messages)
Example #5
0
  def _UpdateOrCreateService(self, service_ref, config_changes, with_code):
    """Apply config_changes to the service. Create it if necessary.

    Arguments:
      service_ref: Reference to the service to create or update
      config_changes: list of ConfigChanger to modify the service with
      with_code: boolean, True if the config_changes contains code to deploy.
        We can't create the service if we're not deploying code.

    Returns:
      The Service object we created or modified.
    """
    nonce = _Nonce()
    config_changes = [_NewRevisionForcingChange(nonce)] + config_changes
    messages = self._messages_module
    # GET the Service
    serv = self.GetService(service_ref)
    try:
      if serv:
        if not with_code:
          # Avoid changing the running code by making the new revision by digest
          self._EnsureImageDigest(serv, config_changes)
        # PUT the changed Service
        for config_change in config_changes:
          config_change.AdjustConfiguration(serv.configuration, serv.metadata)
        serv_name = service_ref.RelativeName()
        serv_update_req = (
            messages.ServerlessNamespacesServicesReplaceServiceRequest(
                service=serv.Message(),
                name=serv_name))
        with metrics.record_duration(metrics.UPDATE_SERVICE):
          updated = self._client.namespaces_services.ReplaceService(
              serv_update_req)
        return service.Service(updated, messages)

      else:
        if not with_code:
          raise serverless_exceptions.ServiceNotFoundError(
              'Service [{}] could not be found.'.format(service_ref.servicesId))
        # POST a new Service
        new_serv = service.Service.New(self._client, service_ref.namespacesId)
        new_serv.name = service_ref.servicesId
        pretty_print.Info('Creating new service [{bold}{service}{reset}]',
                          service=new_serv.name)
        parent = service_ref.Parent().RelativeName()
        for config_change in config_changes:
          config_change.AdjustConfiguration(new_serv.configuration,
                                            new_serv.metadata)
        serv_create_req = (
            messages.ServerlessNamespacesServicesCreateRequest(
                service=new_serv.Message(),
                parent=parent))
        with metrics.record_duration(metrics.CREATE_SERVICE):
          raw_service = self._client.namespaces_services.Create(
              serv_create_req)
        return service.Service(raw_service, messages)
    except api_exceptions.HttpBadRequestError as e:
      error_payload = exceptions_util.HttpErrorPayload(e)
      if error_payload.field_violations:
        if (serverless_exceptions.BadImageError.IMAGE_ERROR_FIELD
            in error_payload.field_violations):
          exceptions.reraise(serverless_exceptions.BadImageError(e))
      exceptions.reraise(e)