# Copyright 2015 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.

"""Messages for the Machine Provider API."""

# pylint: disable=unused-wildcard-import, wildcard-import

from protorpc import messages

from components.machine_provider.dimensions import *
from components.machine_provider.instructions import *
from components.machine_provider.policies import *


class CatalogMachineRetrievalRequest(messages.Message):
  """Represents a request to retrieve a machine from the catalog."""
  # Hostname of the machine to retrieve.
  hostname = messages.StringField(1, required=True)
  # Backend which added the machine.
  backend = messages.EnumField(Backend, 2)


class CatalogMachineRetrievalResponse(messages.Message):
  """Represents a response to a catalog machine retrieval request."""
  # Dimensions instance specifying what sort of machine this is.
  dimensions = messages.MessageField(Dimensions, 1)
  # Policies governing this machine.
  policies = messages.MessageField(Policies, 2)
  # State of the CatalogMachineEntry.
  state = messages.StringField(3)
  # Cloud Pub/Sub subscription the machine must listen to for instructions.
  pubsub_subscription = messages.StringField(4)
  # Project the Cloud Pub/Sub subscription exists in.
  pubsub_subscription_project = messages.StringField(5)
  # Cloud Pub/Sub topic the machine must be subscribed to.
  pubsub_topic = messages.StringField(6)
  # Project the Cloud Pub/Sub topic exists in.
  pubsub_topic_project = messages.StringField(7)
  # Timestamp indicating lease expiration seconds from epoch in UTC.
  lease_expiration_ts = messages.IntegerField(8)


class CatalogMachineAdditionRequest(messages.Message):
  """Represents a request to add a machine to the catalog.

  dimensions.backend must be specified.
  dimensions.hostname must be unique per backend.
  """
  # Dimensions instance specifying what sort of machine this is.
  dimensions = messages.MessageField(Dimensions, 1, required=True)
  # Policies instance specifying machine-specific configuration.
  policies = messages.MessageField(Policies, 2, required=True)


class CatalogMachineBatchAdditionRequest(messages.Message):
  """Represents a batched set of CatalogMachineAdditionRequests.

  dimensions.backend must be specified in each CatalogMachineAdditionRequest.
  dimensions.hostname must be unique per backend.
  """
  # CatalogMachineAdditionRequest instances to batch together.
  requests = messages.MessageField(
      CatalogMachineAdditionRequest, 1, repeated=True)


class CatalogMachineDeletionRequest(messages.Message):
  """Represents a request to delete a machine in the catalog."""
  # Dimensions instance specifying what sort of machine this is.
  dimensions = messages.MessageField(Dimensions, 1, required=True)


class CatalogManipulationRequestError(messages.Enum):
  """Represents an error in a catalog manipulation request."""
  # Per backend, hostnames must be unique in the catalog.
  HOSTNAME_REUSE = 1
  # Tried to lookup an entry that didn't exist.
  ENTRY_NOT_FOUND = 2
  # Didn't specify a backend.
  UNSPECIFIED_BACKEND = 3
  # Specified backend didn't match the backend originating the request.
  MISMATCHED_BACKEND = 4
  # Didn't specify a hostname.
  UNSPECIFIED_HOSTNAME = 5
  # Proposed Cloud Pub/Sub topic was invalid.
  INVALID_TOPIC = 6
  # Proposed Cloud Pub/Sub project was invalid.
  INVALID_PROJECT = 7
  # Didn't specify a Cloud Pub/Sub topic.
  UNSPECIFIED_TOPIC = 8
  # Attempted to delete a leased machine.
  LEASED = 9


class CatalogManipulationResponse(messages.Message):
  """Represents a response to a catalog manipulation request."""
  # CatalogManipulationRequestError instance indicating an error with the
  # request, or None if there is no error.
  error = messages.EnumField(CatalogManipulationRequestError, 1)
  # CatalogMachineAdditionRequest this response is in reference to.
  machine_addition_request = messages.MessageField(
      CatalogMachineAdditionRequest, 2)
  # CatalogMachineDeletionRequest this response is in reference to.
  machine_deletion_request = messages.MessageField(
      CatalogMachineDeletionRequest, 3)


class CatalogBatchManipulationResponse(messages.Message):
  """Represents a response to a batched catalog manipulation request."""
  responses = messages.MessageField(
      CatalogManipulationResponse, 1, repeated=True)


class LeaseRequest(messages.Message):
  """Represents a request for a lease on a machine."""
  # Per-user unique ID used to deduplicate requests.
  request_id = messages.StringField(1, required=True)
  # Dimensions instance specifying what sort of machine to lease.
  dimensions = messages.MessageField(Dimensions, 2, required=True)
  # Desired length of the lease in seconds.
  duration = messages.IntegerField(3)
  # Cloud Pub/Sub topic name to communicate on regarding this request.
  pubsub_topic = messages.StringField(4)
  # Cloud Pub/Sub project name to communicate on regarding this request.
  pubsub_project = messages.StringField(5)
  # Instructions to give the machine once it's been leased.
  on_lease = messages.MessageField(Instruction, 6)
  # UTC seconds from epoch when lease should expire.
  lease_expiration_ts = messages.IntegerField(7)


class BatchedLeaseRequest(messages.Message):
  """Represents a batched set of LeaseRequests."""
  # LeaseRequest instances to batch together.
  requests = messages.MessageField(LeaseRequest, 1, repeated=True)


class LeaseRequestError(messages.Enum):
  """Represents an error in a LeaseRequest."""
  # Request IDs are intended to be unique.
  # Reusing a request ID in a different request is an error.
  REQUEST_ID_REUSE = 1
  # Proposed Cloud Pub/Sub topic was invalid.
  INVALID_TOPIC = 2
  # Proposed Cloud Pub/Sub project was invalid.
  INVALID_PROJECT = 3
  # Didn't specify a Cloud Pub/Sub topic.
  UNSPECIFIED_TOPIC = 4
  # Request couldn't be processed in time.
  DEADLINE_EXCEEDED = 5
  # Miscellaneous transient error.
  TRANSIENT_ERROR = 6
  # Mutually exclusive duration and lease_expiration_ts both specified.
  MUTUAL_EXCLUSION_ERROR = 7
  # Proposed duration was zero or negative.
  NONPOSITIVE_DEADLINE = 8
  # Proposed expiration time is not in the future.
  LEASE_EXPIRATION_TS_ERROR = 9
  # Neither duration nor lease_expiration_ts were specified.
  LEASE_LENGTH_UNSPECIFIED = 10
  # Requested lease duration is too long.
  LEASE_TOO_LONG = 11


class LeaseRequestState(messages.Enum):
  """Represents the state of a LeaseRequest."""
  # LeaseRequest has been received, but not processed yet.
  UNTRIAGED = 0
  # LeaseRequest is pending provisioning of additional capacity.
  PENDING = 1
  # LeaseRequest has been fulfilled.
  FULFILLED = 2
  # LeaseRequest has been denied.
  DENIED = 3


class LeaseResponse(messages.Message):
  """Represents a response to a LeaseRequest."""
  # SHA-1 identifying the LeaseRequest this response refers to.
  request_hash = messages.StringField(1)
  # LeaseRequestError instance indicating an error with the request, or None
  # if there is no error.
  error = messages.EnumField(LeaseRequestError, 2)
  # Request ID used by the client to generate the LeaseRequest.
  client_request_id = messages.StringField(3, required=True)
  # State of the LeaseRequest.
  state = messages.EnumField(LeaseRequestState, 4)
  # Hostname of the machine available for this request.
  hostname = messages.StringField(5)
  # Timestamp indicating lease expiration seconds from epoch in UTC.
  lease_expiration_ts = messages.IntegerField(6)


class BatchedLeaseResponse(messages.Message):
  """Represents a response to a batched lease request."""
  responses = messages.MessageField(LeaseResponse, 1, repeated=True)


class LeaseReleaseRequest(messages.Message):
  """Represents a request to voluntarily cancel a LeaseRequest."""
  # Per-user unique ID used to identify the LeaseRequest.
  request_id = messages.StringField(1, required=True)


class BatchedLeaseReleaseRequest(messages.Message):
  """Represents a batched set of lease release requests."""
  requests = messages.MessageField(LeaseReleaseRequest, 1, repeated=True)


class LeaseReleaseRequestError(messages.Enum):
  """Represents an error in a LeaseReleaseRequest."""
  # Request ID referred to non-existent request for this user.
  NOT_FOUND = 1
  # Request ID referred to an unfulfilled request.
  NOT_FULFILLED = 2
  # Request ID referred to a fulfilled request whose machine was
  # already reclaimed.
  ALREADY_RECLAIMED = 3
  # Request couldn't be processed in time.
  DEADLINE_EXCEEDED = 4
  # Miscellaneous transient error.
  TRANSIENT_ERROR = 5


class LeaseReleaseResponse(messages.Message):
  """Represents a response to a LeaseReleaseRequest."""
  # SHA-1 identifying the LeaseRequest this response refers to.
  request_hash = messages.StringField(1)
  # LeaseReleaseRequestError indicating an error with the request, or None
  # if there is no error.
  error = messages.EnumField(LeaseReleaseRequestError, 2)
  # Request ID used by the client to generate the LeaseRequest
  # referred to by the LeaseReleaseRequest.
  client_request_id = messages.StringField(3, required=True)


class BatchedLeaseReleaseResponse(messages.Message):
  """Represents responses to a batched set of lease release requests."""
  responses = messages.MessageField(LeaseReleaseResponse, 1, repeated=True)


class MachineInstructionRequest(messages.Message):
  """Represents a request to send an instruction to a leased machine."""
  # Request ID for the fulfilled LeaseRequest whose machine should be
  # instructed.
  request_id = messages.StringField(1, required=True)
  # Instruction to send the leased machine.
  instruction = messages.MessageField(Instruction, 2)


class MachineInstructionError(messages.Enum):
  """Represents an error in a MachineInstructionRequest."""
  # Request ID referred to an unfulfilled request.
  NOT_FULFILLED = 1
  # Request ID referred to a fulfilled request whose machine was
  # already reclaimed.
  ALREADY_RECLAIMED = 2
  # Invalid instruction for the machine.
  INVALID_INSTRUCTION = 3


class MachineInstructionResponse(messages.Message):
  """Represents a response to a MachineInstructionRequest."""
  # Request ID used by the client to generate the LeaseRequest for the
  # machine being instructed.
  client_request_id = messages.StringField(1, required=True)
  # MachineInstructionError indicating an error with the request, or None
  # if there is no error.
  error = messages.EnumField(MachineInstructionError, 2)


class PollRequest(messages.Message):
  """Represents a request to poll for instructions given to a machine."""
  # Hostname of the machine whose instructions to retrieve.
  hostname = messages.StringField(1, required=True)
  # Backend the machine belongs to. Generally required.
  backend = messages.EnumField(Backend, 2)


class PollResponse(messages.Message):
  """Represents a response to a request for instructions given to a machine."""
  # Instruction given to the machine.
  instruction = messages.MessageField(Instruction, 1)
  # State of the instruction.
  state = messages.StringField(2)


class AckRequest(messages.Message):
  """Represents a request to ack an instruction received by a machine."""
  # Hostname of the machine whose instruction to ack.
  hostname = messages.StringField(1, required=True)
  # Backend the machine belongs to.
  backend = messages.EnumField(Backend, 2)

[((650, 688), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {'required': '(True)'}), '(1, required=True)\n', (670, 688), False, 'from protorpc import messages\n'), ((738, 768), 'protorpc.messages.EnumField', 'messages.EnumField', (['Backend', '(2)'], {}), '(Backend, 2)\n', (756, 768), False, 'from protorpc import messages\n'), ((978, 1014), 'protorpc.messages.MessageField', 'messages.MessageField', (['Dimensions', '(1)'], {}), '(Dimensions, 1)\n', (999, 1014), False, 'from protorpc import messages\n'), ((1065, 1099), 'protorpc.messages.MessageField', 'messages.MessageField', (['Policies', '(2)'], {}), '(Policies, 2)\n', (1086, 1099), False, 'from protorpc import messages\n'), ((1148, 1171), 'protorpc.messages.StringField', 'messages.StringField', (['(3)'], {}), '(3)\n', (1168, 1171), False, 'from protorpc import messages\n'), ((1272, 1295), 'protorpc.messages.StringField', 'messages.StringField', (['(4)'], {}), '(4)\n', (1292, 1295), False, 'from protorpc import messages\n'), ((1382, 1405), 'protorpc.messages.StringField', 'messages.StringField', (['(5)'], {}), '(5)\n', (1402, 1405), False, 'from protorpc import messages\n'), ((1482, 1505), 'protorpc.messages.StringField', 'messages.StringField', (['(6)'], {}), '(6)\n', (1502, 1505), False, 'from protorpc import messages\n'), ((1578, 1601), 'protorpc.messages.StringField', 'messages.StringField', (['(7)'], {}), '(7)\n', (1598, 1601), False, 'from protorpc import messages\n'), ((1695, 1719), 'protorpc.messages.IntegerField', 'messages.IntegerField', (['(8)'], {}), '(8)\n', (1716, 1719), False, 'from protorpc import messages\n'), ((2013, 2064), 'protorpc.messages.MessageField', 'messages.MessageField', (['Dimensions', '(1)'], {'required': '(True)'}), '(Dimensions, 1, required=True)\n', (2034, 2064), False, 'from protorpc import messages\n'), ((2143, 2192), 'protorpc.messages.MessageField', 'messages.MessageField', (['Policies', '(2)'], {'required': '(True)'}), '(Policies, 2, required=True)\n', (2164, 2192), False, 'from protorpc import messages\n'), ((2531, 2601), 'protorpc.messages.MessageField', 'messages.MessageField', (['CatalogMachineAdditionRequest', '(1)'], {'repeated': '(True)'}), '(CatalogMachineAdditionRequest, 1, repeated=True)\n', (2552, 2601), False, 'from protorpc import messages\n'), ((2811, 2862), 'protorpc.messages.MessageField', 'messages.MessageField', (['Dimensions', '(1)'], {'required': '(True)'}), '(Dimensions, 1, required=True)\n', (2832, 2862), False, 'from protorpc import messages\n'), ((3846, 3900), 'protorpc.messages.EnumField', 'messages.EnumField', (['CatalogManipulationRequestError', '(1)'], {}), '(CatalogManipulationRequestError, 1)\n', (3864, 3900), False, 'from protorpc import messages\n'), ((3998, 4053), 'protorpc.messages.MessageField', 'messages.MessageField', (['CatalogMachineAdditionRequest', '(2)'], {}), '(CatalogMachineAdditionRequest, 2)\n', (4019, 4053), False, 'from protorpc import messages\n'), ((4158, 4213), 'protorpc.messages.MessageField', 'messages.MessageField', (['CatalogMachineDeletionRequest', '(3)'], {}), '(CatalogMachineDeletionRequest, 3)\n', (4179, 4213), False, 'from protorpc import messages\n'), ((4368, 4436), 'protorpc.messages.MessageField', 'messages.MessageField', (['CatalogManipulationResponse', '(1)'], {'repeated': '(True)'}), '(CatalogManipulationResponse, 1, repeated=True)\n', (4389, 4436), False, 'from protorpc import messages\n'), ((4607, 4645), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {'required': '(True)'}), '(1, required=True)\n', (4627, 4645), False, 'from protorpc import messages\n'), ((4727, 4778), 'protorpc.messages.MessageField', 'messages.MessageField', (['Dimensions', '(2)'], {'required': '(True)'}), '(Dimensions, 2, required=True)\n', (4748, 4778), False, 'from protorpc import messages\n'), ((4836, 4860), 'protorpc.messages.IntegerField', 'messages.IntegerField', (['(3)'], {}), '(3)\n', (4857, 4860), False, 'from protorpc import messages\n'), ((4949, 4972), 'protorpc.messages.StringField', 'messages.StringField', (['(4)'], {}), '(4)\n', (4969, 4972), False, 'from protorpc import messages\n'), ((5065, 5088), 'protorpc.messages.StringField', 'messages.StringField', (['(5)'], {}), '(5)\n', (5085, 5088), False, 'from protorpc import messages\n'), ((5162, 5199), 'protorpc.messages.MessageField', 'messages.MessageField', (['Instruction', '(6)'], {}), '(Instruction, 6)\n', (5183, 5199), False, 'from protorpc import messages\n'), ((5277, 5301), 'protorpc.messages.IntegerField', 'messages.IntegerField', (['(7)'], {}), '(7)\n', (5298, 5301), False, 'from protorpc import messages\n'), ((5459, 5512), 'protorpc.messages.MessageField', 'messages.MessageField', (['LeaseRequest', '(1)'], {'repeated': '(True)'}), '(LeaseRequest, 1, repeated=True)\n', (5480, 5512), False, 'from protorpc import messages\n'), ((6982, 7005), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {}), '(1)\n', (7002, 7005), False, 'from protorpc import messages\n'), ((7119, 7159), 'protorpc.messages.EnumField', 'messages.EnumField', (['LeaseRequestError', '(2)'], {}), '(LeaseRequestError, 2)\n', (7137, 7159), False, 'from protorpc import messages\n'), ((7246, 7284), 'protorpc.messages.StringField', 'messages.StringField', (['(3)'], {'required': '(True)'}), '(3, required=True)\n', (7266, 7284), False, 'from protorpc import messages\n'), ((7326, 7366), 'protorpc.messages.EnumField', 'messages.EnumField', (['LeaseRequestState', '(4)'], {}), '(LeaseRequestState, 4)\n', (7344, 7366), False, 'from protorpc import messages\n'), ((7436, 7459), 'protorpc.messages.StringField', 'messages.StringField', (['(5)'], {}), '(5)\n', (7456, 7459), False, 'from protorpc import messages\n'), ((7553, 7577), 'protorpc.messages.IntegerField', 'messages.IntegerField', (['(6)'], {}), '(6)\n', (7574, 7577), False, 'from protorpc import messages\n'), ((7698, 7752), 'protorpc.messages.MessageField', 'messages.MessageField', (['LeaseResponse', '(1)'], {'repeated': '(True)'}), '(LeaseResponse, 1, repeated=True)\n', (7719, 7752), False, 'from protorpc import messages\n'), ((7940, 7978), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {'required': '(True)'}), '(1, required=True)\n', (7960, 7978), False, 'from protorpc import messages\n'), ((8106, 8166), 'protorpc.messages.MessageField', 'messages.MessageField', (['LeaseReleaseRequest', '(1)'], {'repeated': '(True)'}), '(LeaseReleaseRequest, 1, repeated=True)\n', (8127, 8166), False, 'from protorpc import messages\n'), ((8841, 8864), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {}), '(1)\n', (8861, 8864), False, 'from protorpc import messages\n'), ((8976, 9023), 'protorpc.messages.EnumField', 'messages.EnumField', (['LeaseReleaseRequestError', '(2)'], {}), '(LeaseReleaseRequestError, 2)\n', (8994, 9023), False, 'from protorpc import messages\n'), ((9153, 9191), 'protorpc.messages.StringField', 'messages.StringField', (['(3)'], {'required': '(True)'}), '(3, required=True)\n', (9173, 9191), False, 'from protorpc import messages\n'), ((9334, 9395), 'protorpc.messages.MessageField', 'messages.MessageField', (['LeaseReleaseResponse', '(1)'], {'repeated': '(True)'}), '(LeaseReleaseResponse, 1, repeated=True)\n', (9355, 9395), False, 'from protorpc import messages\n'), ((9623, 9661), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {'required': '(True)'}), '(1, required=True)\n', (9643, 9661), False, 'from protorpc import messages\n'), ((9722, 9759), 'protorpc.messages.MessageField', 'messages.MessageField', (['Instruction', '(2)'], {}), '(Instruction, 2)\n', (9743, 9759), False, 'from protorpc import messages\n'), ((10357, 10395), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {'required': '(True)'}), '(1, required=True)\n', (10377, 10395), False, 'from protorpc import messages\n'), ((10506, 10552), 'protorpc.messages.EnumField', 'messages.EnumField', (['MachineInstructionError', '(2)'], {}), '(MachineInstructionError, 2)\n', (10524, 10552), False, 'from protorpc import messages\n'), ((10739, 10777), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {'required': '(True)'}), '(1, required=True)\n', (10759, 10777), False, 'from protorpc import messages\n'), ((10846, 10876), 'protorpc.messages.EnumField', 'messages.EnumField', (['Backend', '(2)'], {}), '(Backend, 2)\n', (10864, 10876), False, 'from protorpc import messages\n'), ((11051, 11088), 'protorpc.messages.MessageField', 'messages.MessageField', (['Instruction', '(1)'], {}), '(Instruction, 1)\n', (11072, 11088), False, 'from protorpc import messages\n'), ((11129, 11152), 'protorpc.messages.StringField', 'messages.StringField', (['(2)'], {}), '(2)\n', (11149, 11152), False, 'from protorpc import messages\n'), ((11332, 11370), 'protorpc.messages.StringField', 'messages.StringField', (['(1)'], {'required': '(True)'}), '(1, required=True)\n', (11352, 11370), False, 'from protorpc import messages\n'), ((11419, 11449), 'protorpc.messages.EnumField', 'messages.EnumField', (['Backend', '(2)'], {}), '(Backend, 2)\n', (11437, 11449), False, 'from protorpc import messages\n')]