syntax = "proto3";

package code.justin.tv.eventbus.controlplane;
option go_package = "rpc";

import "code.justin.tv/eventbus/controlplane/rpc/event_streams.proto";

service Services {
  // List the services available in the system. Does not include statistics.
  rpc List(ListServicesReq) returns (ListServicesResp);

  // Get details of a single service
  rpc Get(GetServiceReq) returns (Service);

  // Create a service
  rpc Create(CreateServiceReq) returns (CreateServiceResp);

  // Update a service
  rpc Update(UpdateServiceReq) returns (UpdateServiceResp);

  // Get a service's authorized field grants
  rpc GetAuthorizedFieldGrants(GetAuthorizedFieldGrantsReq) returns (GetAuthorizedFieldGrantsResp);

  // Get requestor's LDAP groups
  rpc RequestorLDAPGroups(RequestorLDAPGroupsReq) returns (RequestorLDAPGroupsResp);

  // Get list of members of a given LDAP group
  rpc ResolveLDAPGroupUsers(ResolveLDAPGroupUsersReq) returns (ResolveLDAPGroupUsersResp);

  rpc DeleteIAMRole(DeleteIAMRoleReq) returns (DeleteIAMRoleResp);

  rpc GetServicesForEventStream(GetServicesForEventStreamReq) returns (GetServicesForEventStreamResp);

  rpc GetCloudFormationVersion(GetCloudFormationVersionReq) returns (GetCloudFormationVersionResp);

  rpc CreateIAMRole(CreateIAMRoleReq) returns (CreateIAMRoleResp);

  rpc UpdateIAMRoleLabel(UpdateIAMRoleLabelReq) returns (UpdateIAMRoleLabelResp);

  rpc ValidateIAMRole(ValidateIAMRoleReq) returns (ValidateIAMRoleResp);
}

message ListServicesReq {}

message ListServicesResp {
  repeated Service services = 1;
}

message GetServiceReq {
  string id = 1;
}

message CreateServiceReq {
  Service service = 1;
}

message CreateServiceResp {
  Service service = 1;
}

message UpdateServiceReq {
  Service service = 1;
}

message UpdateServiceResp {
  Service service = 1;
}

message Service {
  string id = 1; // A canonical service ID, e.g. "123456"
  string name = 2; // Display name, e.g. "WebSub"

  // The following are not included when listing services
  string service_catalog_url = 3;
  string description = 4; // Long description. May contain markdown-formatted paragraphs and links.

  repeated Account accounts = 5 [deprecated=true];

  string ldap_group = 6; // The LDAP group which owns the service

  repeated IAMRole iam_roles = 7;
}

message Account {
  string id = 1;
  string label = 2;

  // The following are included only on list services
  string cloudformation_status = 3;
}

// All relevant request information will be inherent in the 
// requestor's context (provided by LDAP/auth middlewares)
message RequestorLDAPGroupsReq {}

message RequestorLDAPGroupsResp {
  repeated string ldap_groups = 1;
}

message ResolveLDAPGroupUsersReq {
  string ldap_group = 1;
}

message ResolveLDAPGroupUsersResp {
  repeated LDAPEntry ldap_entries = 1;
}

message LDAPEntry {
  // The person's human readable name
  string common_name = 1;
  // The person's LDAP login
  string uid = 2;
}

message GetAuthorizedFieldGrantsReq {
  string service_id = 1;
}

message GetAuthorizedFieldGrantsResp {
  repeated AuthorizedFieldSubscriberGrant subscriber_grants = 1;
  repeated AuthorizedFieldPublisherGrant publisher_grants = 2;
}

message AuthorizedFieldPublisherGrant {
  IAMRole iam_role = 1;
  string event_type = 2;
  string environment = 3;
}

message DeleteIAMRoleReq {
  string arn = 1;
}

message DeleteIAMRoleResp {}

message GetServicesForEventStreamReq {
  string event_type = 1;
}

message GetServicesForEventStreamResp {
  repeated Service services = 1;
}

message GetCloudFormationVersionReq {
  IAMRole iam_role = 1; 
}

message GetCloudFormationVersionResp {
  string version = 1;
}

message CreateIAMRoleReq {
  string service_id = 1;
  IAMRole iam_role = 2;
}

message CreateIAMRoleResp {
  IAMRole iam_role = 2;
}

message UpdateIAMRoleLabelReq {
  string arn = 1;
  string label = 2;
}

message UpdateIAMRoleLabelResp {
  string arn = 1;
  string label = 2;
}

message ValidateIAMRoleReq {
  string arn = 1;
}

message ValidateIAMRoleResp {
  bool is_valid = 1;
  string message = 2; 
}

message IAMRole {
  string arn = 1;
  string label = 2;

  // The following are included only on list services
  string cloudformation_status = 3;
  string id = 4;
}

message AuthorizedFieldSubscriberGrant {
  IAMRole iam_role = 1;
  string event_type = 2;
  string environment = 3;
  AuthorizedField authorized_field = 4;
}
