syntax = "proto3";

package controller.api.services.v1;

option go_package = "github.com/hashicorp/boundary/internal/gen/controller/api/services;services";

import "protoc-gen-openapiv2/options/annotations.proto";
import "google/api/annotations.proto";
import "google/protobuf/field_mask.proto";
import "google/protobuf/wrappers.proto";
import "controller/api/resources/groups/v1/group.proto";
import "controller/api/resources/scopes/v1/scope.proto";

service GroupService {

  // GetGroup returns a stored Group if present. The provided request must
  // include the Group id and if it is missing, malformed or referencing a
  // non existing resource an error is returned.
  rpc GetGroup(GetGroupRequest) returns (GetGroupResponse) {
    option (google.api.http) = {
      get: "/v1/groups/{id}"
      response_body: "item"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Gets a single Group."
    };
  }

  // ListGroups returns a list of stored Groups which exist inside the
  // provided scope id. If that id is missing, malformed, or
  // references a non-existing scope, an error is returned.
  rpc ListGroups(ListGroupsRequest) returns (ListGroupsResponse) {
    option (google.api.http) = {
      get: "/v1/groups"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Lists all Groups."
    };
  }

  // CreateGroup creates and stores a Group in boundary. The provided
  // request must include the scope ID in which the Group will be created.
  // If the scope ID is missing, malformed or references a non existing
  // resource, an error is returned. If a name is provided that is in
  // use in another Group in the same scope, an error is returned.
  rpc CreateGroup(CreateGroupRequest) returns (CreateGroupResponse) {
    option (google.api.http) = {
      post: "/v1/groups"
      body: "item"
      response_body: "item"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Creates a single Group."
    };
  }

  // UpdateGroup updates an existing Group in boundary. The provided Group
  // must not have any read only fields set. The update mask must be included in
  // the request and contain at least 1 mutable field. To unset a field's value,
  // include the field in the update mask and don't set it in the provided
  // Group. An error is returned if the Group id is missing or reference a
  // non-existing resource. An error is also returned if the request attempts
  // to update the name to one that is already used by another Group in the
  // same scope.
  rpc UpdateGroup(UpdateGroupRequest) returns (UpdateGroupResponse) {
    option (google.api.http) = {
      patch: "/v1/groups/{id}"
      body: "item"
      response_body: "item"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Updates a Group."
    };
  }

  // DeleteGroup removes a Group from Boundary. If the provided Group
  // ID is malformed or not provided an error is returned.
  rpc DeleteGroup(DeleteGroupRequest) returns (DeleteGroupResponse) {
    option (google.api.http) = {
      delete: "/v1/groups/{id}"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Deletes a Group."
    };
  }

  // AddGroupMembers adds members to a Group.  The provided request must include
  // the scope id and the Group id which the members will be added to.
  // An error is returned if any provided id is missing, malformed or
  // references a non-existing resource.
  rpc AddGroupMembers(AddGroupMembersRequest) returns (AddGroupMembersResponse) {
    option (google.api.http) = {
      post: "/v1/groups/{id}:add-members"
      body: "*"
      response_body: "item"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Adds members to a Group."
    };
  }

  // SetGroupMembers sets the Group's members.  Any existing members on
  // the Group are deleted if they are not included in this request.  The provided
  // request must include the Group id which the members will be set to.
  // An error is returned if any provided id is missing, malformed or
  // references a non-existing resource.
  rpc SetGroupMembers(SetGroupMembersRequest) returns (SetGroupMembersResponse) {
    option (google.api.http) = {
      post: "/v1/groups/{id}:set-members"
      body: "*"
      response_body: "item"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Set a Group's members to exactly the list of provided in the request, removing any members that are not specified."
    };
  }

  // RemoveGroupMembers removes members from the specified Group.
  // The provided request must include the the Group id to which the members
  // will be set.
  // An error is returned if any provided id is missing, malformed or
  // references a non-existing resource.
  rpc RemoveGroupMembers(RemoveGroupMembersRequest) returns (RemoveGroupMembersResponse) {
    option (google.api.http) = {
      post: "/v1/groups/{id}:remove-members"
      body: "*"
      response_body: "item"
    };
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Removes the specified members from a Group."
    };
  }
}

message GetGroupRequest {
  string id = 1;
}

message GetGroupResponse {
  resources.groups.v1.Group item = 1;
}

message ListGroupsRequest {
  string scope_id = 1 [json_name="scope_id"];
}

message ListGroupsResponse {
  repeated resources.groups.v1.Group items = 1;
}

message CreateGroupRequest {
  resources.groups.v1.Group item = 1;
}

message CreateGroupResponse {
  string uri = 1;
  resources.groups.v1.Group item = 2;
}

message UpdateGroupRequest {
  string id = 1;
  resources.groups.v1.Group item = 2;
  google.protobuf.FieldMask update_mask = 3 [json_name="update_mask"];
}

message UpdateGroupResponse {
  resources.groups.v1.Group item = 1;
}

message DeleteGroupRequest {
  string id = 1;
}

message DeleteGroupResponse {}

message AddGroupMembersRequest {
  string id = 1;
  // Version is used to ensure this resource has not changed.
  // The mutation will fail if the version does not match the latest known good version.
  uint32 version = 2;
  repeated string member_ids = 3 [json_name="member_ids"];
}

message AddGroupMembersResponse {
  resources.groups.v1.Group item = 1;
}

message SetGroupMembersRequest {
  string id = 1;
  // Version is used to ensure this resource has not changed.
  // The mutation will fail if the version does not match the latest known good version.
  uint32 version = 2;
  repeated string member_ids = 3 [json_name="member_ids"];
}

message SetGroupMembersResponse {
  resources.groups.v1.Group item = 1;
}

message RemoveGroupMembersRequest {
  string id = 1;
  // Version is used to ensure this resource has not changed.
  // The mutation will fail if the version does not match the latest known good version.
  uint32 version = 2;
  repeated string member_ids = 3 [json_name="member_ids"];
}

message RemoveGroupMembersResponse {
  resources.groups.v1.Group item = 1;
}
