66
models/admin_info_response.go
Normal file
66
models/admin_info_response.go
Normal file
@@ -0,0 +1,66 @@
|
||||
// Code generated by go-swagger; DO NOT EDIT.
|
||||
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package models
|
||||
|
||||
// This file was generated by the swagger tool.
|
||||
// Editing this file might prove futile when you re-run the swagger generate command
|
||||
|
||||
import (
|
||||
"github.com/go-openapi/strfmt"
|
||||
"github.com/go-openapi/swag"
|
||||
)
|
||||
|
||||
// AdminInfoResponse admin info response
|
||||
//
|
||||
// swagger:model adminInfoResponse
|
||||
type AdminInfoResponse struct {
|
||||
|
||||
// buckets
|
||||
Buckets int64 `json:"buckets,omitempty"`
|
||||
|
||||
// objects
|
||||
Objects int64 `json:"objects,omitempty"`
|
||||
|
||||
// usage
|
||||
Usage int64 `json:"usage,omitempty"`
|
||||
}
|
||||
|
||||
// Validate validates this admin info response
|
||||
func (m *AdminInfoResponse) Validate(formats strfmt.Registry) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalBinary interface implementation
|
||||
func (m *AdminInfoResponse) MarshalBinary() ([]byte, error) {
|
||||
if m == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return swag.WriteJSON(m)
|
||||
}
|
||||
|
||||
// UnmarshalBinary interface implementation
|
||||
func (m *AdminInfoResponse) UnmarshalBinary(b []byte) error {
|
||||
var res AdminInfoResponse
|
||||
if err := swag.ReadJSON(b, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
*m = res
|
||||
return nil
|
||||
}
|
||||
88
restapi/admin_info.go
Normal file
88
restapi/admin_info.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package restapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/go-openapi/runtime/middleware"
|
||||
"github.com/go-openapi/swag"
|
||||
"github.com/minio/m3/mcs/models"
|
||||
"github.com/minio/m3/mcs/restapi/operations"
|
||||
"github.com/minio/m3/mcs/restapi/operations/admin_api"
|
||||
)
|
||||
|
||||
func registerAdminInfoHandlers(api *operations.McsAPI) {
|
||||
// session check
|
||||
api.AdminAPIAdminInfoHandler = admin_api.AdminInfoHandlerFunc(func(params admin_api.AdminInfoParams, principal *models.Principal) middleware.Responder {
|
||||
infoResp, err := getAdminInfoResponse()
|
||||
if err != nil {
|
||||
return admin_api.NewAdminInfoDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())})
|
||||
}
|
||||
return admin_api.NewAdminInfoOK().WithPayload(infoResp)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
type UsageInfo struct {
|
||||
Buckets int64
|
||||
Objects int64
|
||||
Usage int64
|
||||
}
|
||||
|
||||
// getAdminInfo invokes admin info and returns a parsed `UsageInfo` structure
|
||||
func getAdminInfo(ctx context.Context, client MinioAdmin) (*UsageInfo, error) {
|
||||
serverInfo, err := client.serverInfo(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// we are trimming uint64 to int64 this will report an incorrect measurement for numbers greater than
|
||||
// 9,223,372,036,854,775,807
|
||||
return &UsageInfo{
|
||||
Buckets: int64(serverInfo.Buckets.Count),
|
||||
Objects: int64(serverInfo.Objects.Count),
|
||||
Usage: int64(serverInfo.Usage.Size),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// getAdminInfoResponse returns the response containing total buckets, objects and usage.
|
||||
func getAdminInfoResponse() (*models.AdminInfoResponse, error) {
|
||||
mAdmin, err := newMAdminClient()
|
||||
if err != nil {
|
||||
log.Println("error creating Madmin Client:", err)
|
||||
return nil, err
|
||||
}
|
||||
// create a minioClient interface implementation
|
||||
// defining the client to be used
|
||||
adminClient := adminClient{client: mAdmin}
|
||||
// 20 seconds timeout
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20)
|
||||
defer cancel()
|
||||
// serialize output
|
||||
usage, err := getAdminInfo(ctx, adminClient)
|
||||
if err != nil {
|
||||
log.Println("error getting information:", err)
|
||||
return nil, err
|
||||
}
|
||||
sessionResp := &models.AdminInfoResponse{
|
||||
Buckets: usage.Buckets,
|
||||
Objects: usage.Objects,
|
||||
Usage: usage.Usage,
|
||||
}
|
||||
return sessionResp, nil
|
||||
}
|
||||
59
restapi/admin_info_test.go
Normal file
59
restapi/admin_info_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package restapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
asrt "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAdminInfo(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
// Test-1 : getAdminInfo() returns proper information
|
||||
minioServerInfoMock = func(ctx context.Context) (madmin.InfoMessage, error) {
|
||||
return madmin.InfoMessage{
|
||||
Buckets: madmin.Buckets{Count: 10},
|
||||
Objects: madmin.Objects{Count: 10},
|
||||
Usage: madmin.Usage{Size: 10},
|
||||
}, nil
|
||||
}
|
||||
ctx := context.Background()
|
||||
serverInfo, err := getAdminInfo(ctx, adminClient)
|
||||
assert.NotNil(serverInfo, "server info was returned nil")
|
||||
if serverInfo != nil {
|
||||
var actual64 int64 = 10
|
||||
assert.Equal(serverInfo.Buckets, actual64, "Incorrect bucket count")
|
||||
assert.Equal(serverInfo.Objects, actual64, "Incorrect object count")
|
||||
assert.Equal(serverInfo.Usage, actual64, "Incorrect usage size")
|
||||
}
|
||||
assert.Nil(err, "Error should have been nil")
|
||||
|
||||
// Test-2 : getAdminInfo(ctx) fails for whatever reason
|
||||
minioServerInfoMock = func(ctx context.Context) (madmin.InfoMessage, error) {
|
||||
return madmin.InfoMessage{}, errors.New("some reason")
|
||||
}
|
||||
|
||||
serverInfo, err = getAdminInfo(ctx, adminClient)
|
||||
assert.Nil(serverInfo, "server info was not returned nil")
|
||||
assert.NotNil(err, "An error should have ben returned")
|
||||
|
||||
}
|
||||
@@ -28,18 +28,12 @@ import (
|
||||
|
||||
// assigning mock at runtime instead of compile time
|
||||
var minioServiceRestartMock func(ctx context.Context) error
|
||||
var minioServerInfoMock func(ctx context.Context) (madmin.InfoMessage, error)
|
||||
|
||||
// mock function of serviceRestart()
|
||||
func (ac adminClientMock) serviceRestart(ctx context.Context) error {
|
||||
return minioServiceRestartMock(ctx)
|
||||
}
|
||||
|
||||
// mock function of serverInfo()
|
||||
func (ac adminClientMock) serverInfo(ctx context.Context) (madmin.InfoMessage, error) {
|
||||
return minioServerInfoMock(ctx)
|
||||
}
|
||||
|
||||
func TestServiceRestart(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
|
||||
@@ -18,362 +18,19 @@ package restapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
|
||||
"errors"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
asrt "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// assigning mock at runtime instead of compile time
|
||||
var minioListUsersMock func() (map[string]madmin.UserInfo, error)
|
||||
var minioAddUserMock func(accessKey, secreyKey string) error
|
||||
var minioListGroupsMock func() ([]string, error)
|
||||
var minioUpdateGroupMembersMock func(madmin.GroupAddRemove) error
|
||||
var minioGetGroupDescriptionMock func(group string) (*madmin.GroupDesc, error)
|
||||
var minioSetGroupStatusMock func(group string, status madmin.GroupStatus) error
|
||||
|
||||
// Define a mock struct of Admin Client interface implementation
|
||||
type adminClientMock struct{}
|
||||
|
||||
// mock function of listUsers()
|
||||
func (ac adminClientMock) listUsers(ctx context.Context) (map[string]madmin.UserInfo, error) {
|
||||
return minioListUsersMock()
|
||||
}
|
||||
|
||||
// mock function of addUser()
|
||||
func (ac adminClientMock) addUser(ctx context.Context, accessKey, secretKey string) error {
|
||||
return minioAddUserMock(accessKey, secretKey)
|
||||
}
|
||||
|
||||
// mock function of listGroups()
|
||||
func (ac adminClientMock) listGroups(ctx context.Context) ([]string, error) {
|
||||
return minioListGroupsMock()
|
||||
}
|
||||
|
||||
// mock function of updateGroupMembers()
|
||||
func (ac adminClientMock) updateGroupMembers(ctx context.Context, req madmin.GroupAddRemove) error {
|
||||
return minioUpdateGroupMembersMock(req)
|
||||
}
|
||||
|
||||
// mock function of getGroupDescription()
|
||||
func (ac adminClientMock) getGroupDescription(ctx context.Context, group string) (*madmin.GroupDesc, error) {
|
||||
return minioGetGroupDescriptionMock(group)
|
||||
}
|
||||
|
||||
// mock function setGroupStatus()
|
||||
func (ac adminClientMock) setGroupStatus(ctx context.Context, group string, status madmin.GroupStatus) error {
|
||||
return minioSetGroupStatusMock(group, status)
|
||||
}
|
||||
|
||||
func TestListUsers(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
// Test-1 : listUsers() Get response from minio client with two users and return the same number on listUsers()
|
||||
// mock minIO client
|
||||
mockUserMap := map[string]madmin.UserInfo{
|
||||
"ABCDEFGHI": madmin.UserInfo{
|
||||
SecretKey: "",
|
||||
PolicyName: "ABCDEFGHI-policy",
|
||||
Status: "enabled",
|
||||
MemberOf: []string{"group1", "group2"},
|
||||
},
|
||||
"ZBCDEFGHI": madmin.UserInfo{
|
||||
SecretKey: "",
|
||||
PolicyName: "ZBCDEFGHI-policy",
|
||||
Status: "enabled",
|
||||
MemberOf: []string{"group1", "group2"},
|
||||
},
|
||||
}
|
||||
|
||||
// mock function response from listUsersWithContext(ctx)
|
||||
minioListUsersMock = func() (map[string]madmin.UserInfo, error) {
|
||||
return mockUserMap, nil
|
||||
}
|
||||
// get list users response this response should have Name, CreationDate, Size and Access
|
||||
// as part of of each user
|
||||
function := "listUsers()"
|
||||
userMap, err := listUsers(adminClient)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// verify length of users is correct
|
||||
assert.Equal(len(mockUserMap), len(userMap), fmt.Sprintf("Failed on %s: length of user's lists is not the same", function))
|
||||
|
||||
for _, b := range userMap {
|
||||
assert.Contains(mockUserMap, b.AccessKey)
|
||||
assert.Equal(string(mockUserMap[b.AccessKey].Status), b.Status)
|
||||
assert.Equal(mockUserMap[b.AccessKey].PolicyName, b.Policy)
|
||||
assert.ElementsMatch(mockUserMap[b.AccessKey].MemberOf, []string{"group1", "group2"})
|
||||
}
|
||||
|
||||
// Test-2 : listUsers() Return and see that the error is handled correctly and returned
|
||||
minioListUsersMock = func() (map[string]madmin.UserInfo, error) {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
_, err = listUsers(adminClient)
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddUser(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
|
||||
// Test-1: valid case of adding a user with a proper access key
|
||||
accessKey := "ABCDEFGHI"
|
||||
secretKey := "ABCDEFGHIABCDEFGHI"
|
||||
|
||||
// mock function response from addUser() return no error
|
||||
minioAddUserMock = func(accessKey, secretKey string) error {
|
||||
return nil
|
||||
}
|
||||
// adds a valid user to MinIO
|
||||
function := "addUser()"
|
||||
user, err := addUser(adminClient, &accessKey, &secretKey)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// no error should have been returned
|
||||
assert.Nil(err, "Error is not null")
|
||||
// the same access key should be in the model users
|
||||
assert.Equal(user.AccessKey, accessKey)
|
||||
// Test-1: valid case
|
||||
accessKey = "AB"
|
||||
secretKey = "ABCDEFGHIABCDEFGHI"
|
||||
|
||||
// mock function response from addUser() return no error
|
||||
minioAddUserMock = func(accessKey, secretKey string) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
|
||||
user, err = addUser(adminClient, &accessKey, &secretKey)
|
||||
|
||||
// no error should have been returned
|
||||
assert.Nil(user, "User is not null")
|
||||
assert.NotNil(err, "An error should have been returned")
|
||||
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestListGroups(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : listGroups() Get response from minio client with two Groups and return the same number on listGroups()
|
||||
mockGroupsList := []string{"group1", "group2"}
|
||||
|
||||
// mock function response from listGroups()
|
||||
minioListGroupsMock = func() ([]string, error) {
|
||||
return mockGroupsList, nil
|
||||
}
|
||||
// get list Groups response this response should have Name, CreationDate, Size and Access
|
||||
// as part of of each Groups
|
||||
function := "listGroups()"
|
||||
groupsList, err := listGroups(ctx, adminClient)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// verify length of Groupss is correct
|
||||
assert.Equal(len(mockGroupsList), len(*groupsList), fmt.Sprintf("Failed on %s: length of Groups's lists is not the same", function))
|
||||
|
||||
for i, g := range *groupsList {
|
||||
assert.Equal(mockGroupsList[i], g)
|
||||
}
|
||||
|
||||
// Test-2 : listGroups() Return error and see that the error is handled correctly and returned
|
||||
minioListGroupsMock = func() ([]string, error) {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
_, err = listGroups(ctx, adminClient)
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddGroup(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : addGroup() add a new group with two members
|
||||
newGroup := "acmeGroup"
|
||||
groupMembers := []string{"user1", "user2"}
|
||||
// mock function response from updateGroupMembers()
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
function := "addGroup()"
|
||||
if err := addGroup(ctx, adminClient, newGroup, groupMembers); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
// Test-2 : addGroup() Return error and see that the error is handled correctly and returned
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
|
||||
if err := addGroup(ctx, adminClient, newGroup, groupMembers); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveGroup(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : removeGroup() remove group assume it has no members
|
||||
groupToRemove := "acmeGroup"
|
||||
// mock function response from updateGroupMembers()
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
function := "removeGroup()"
|
||||
if err := removeGroup(ctx, adminClient, groupToRemove); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
// Test-2 : removeGroup() Return error and see that the error is handled correctly and returned
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
if err := removeGroup(ctx, adminClient, groupToRemove); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupInfo(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : groupInfo() get group info
|
||||
groupName := "acmeGroup"
|
||||
mockResponse := &madmin.GroupDesc{
|
||||
Name: groupName,
|
||||
Policy: "policyTest",
|
||||
Members: []string{"user1", "user2"},
|
||||
Status: "enabled",
|
||||
}
|
||||
// mock function response from updateGroupMembers()
|
||||
minioGetGroupDescriptionMock = func(group string) (*madmin.GroupDesc, error) {
|
||||
return mockResponse, nil
|
||||
}
|
||||
function := "groupInfo()"
|
||||
info, err := groupInfo(ctx, adminClient, groupName)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
assert.Equal(groupName, info.Name)
|
||||
assert.Equal("policyTest", info.Policy)
|
||||
assert.ElementsMatch([]string{"user1", "user2"}, info.Members)
|
||||
assert.Equal("enabled", info.Status)
|
||||
|
||||
// Test-2 : groupInfo() Return error and see that the error is handled correctly and returned
|
||||
minioGetGroupDescriptionMock = func(group string) (*madmin.GroupDesc, error) {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
_, err = groupInfo(ctx, adminClient, groupName)
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateGroupInfo(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : addOrDeleteMembers() update group members add user3 and delete user2
|
||||
function := "addOrDeleteMembers()"
|
||||
groupName := "acmeGroup"
|
||||
mockGroupDesc := &madmin.GroupDesc{
|
||||
Name: groupName,
|
||||
Policy: "policyTest",
|
||||
Members: []string{"user1", "user2"},
|
||||
Status: "enabled",
|
||||
}
|
||||
membersDesired := []string{"user3", "user1"}
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
// Test-2 : addOrDeleteMembers() handle error correctly
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
|
||||
// Test-3 : addOrDeleteMembers() only add members but handle error on adding
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
membersDesired = []string{"user3", "user1", "user2"}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
|
||||
// Test-4: addOrDeleteMembers() no updates needed so error shall not be triggered or handled.
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
membersDesired = []string{"user1", "user2"}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetGroupStatus(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
function := "setGroupStatus()"
|
||||
groupName := "acmeGroup"
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1: setGroupStatus() update valid disabled status
|
||||
expectedStatus := "disabled"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// Test-2: setGroupStatus() update valid enabled status
|
||||
expectedStatus = "enabled"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// Test-3: setGroupStatus() update invalid status, should send error
|
||||
expectedStatus = "invalid"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); assert.Error(err) {
|
||||
assert.Equal("status not valid", err.Error())
|
||||
}
|
||||
// Test-4: setGroupStatus() handler error correctly
|
||||
expectedStatus = "enabled"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
// Common mocks
|
||||
|
||||
// assigning mock at runtime instead of compile time
|
||||
var minioServerInfoMock func(ctx context.Context) (madmin.InfoMessage, error)
|
||||
|
||||
// mock function of serverInfo()
|
||||
func (ac adminClientMock) serverInfo(ctx context.Context) (madmin.InfoMessage, error) {
|
||||
return minioServerInfoMock(ctx)
|
||||
}
|
||||
|
||||
376
restapi/admin_users_test.go
Normal file
376
restapi/admin_users_test.go
Normal file
@@ -0,0 +1,376 @@
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package restapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
|
||||
"errors"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
asrt "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// assigning mock at runtime instead of compile time
|
||||
var minioListUsersMock func() (map[string]madmin.UserInfo, error)
|
||||
var minioAddUserMock func(accessKey, secreyKey string) error
|
||||
var minioListGroupsMock func() ([]string, error)
|
||||
var minioUpdateGroupMembersMock func(madmin.GroupAddRemove) error
|
||||
var minioGetGroupDescriptionMock func(group string) (*madmin.GroupDesc, error)
|
||||
var minioSetGroupStatusMock func(group string, status madmin.GroupStatus) error
|
||||
|
||||
// mock function of listUsers()
|
||||
func (ac adminClientMock) listUsers(ctx context.Context) (map[string]madmin.UserInfo, error) {
|
||||
return minioListUsersMock()
|
||||
}
|
||||
|
||||
// mock function of addUser()
|
||||
func (ac adminClientMock) addUser(ctx context.Context, accessKey, secretKey string) error {
|
||||
return minioAddUserMock(accessKey, secretKey)
|
||||
}
|
||||
|
||||
// mock function of listGroups()
|
||||
func (ac adminClientMock) listGroups(ctx context.Context) ([]string, error) {
|
||||
return minioListGroupsMock()
|
||||
}
|
||||
|
||||
// mock function of updateGroupMembers()
|
||||
func (ac adminClientMock) updateGroupMembers(ctx context.Context, req madmin.GroupAddRemove) error {
|
||||
return minioUpdateGroupMembersMock(req)
|
||||
}
|
||||
|
||||
// mock function of getGroupDescription()
|
||||
func (ac adminClientMock) getGroupDescription(ctx context.Context, group string) (*madmin.GroupDesc, error) {
|
||||
return minioGetGroupDescriptionMock(group)
|
||||
}
|
||||
|
||||
// mock function setGroupStatus()
|
||||
func (ac adminClientMock) setGroupStatus(ctx context.Context, group string, status madmin.GroupStatus) error {
|
||||
return minioSetGroupStatusMock(group, status)
|
||||
}
|
||||
|
||||
func TestListUsers(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
// Test-1 : listUsers() Get response from minio client with two users and return the same number on listUsers()
|
||||
// mock minIO client
|
||||
mockUserMap := map[string]madmin.UserInfo{
|
||||
"ABCDEFGHI": madmin.UserInfo{
|
||||
SecretKey: "",
|
||||
PolicyName: "ABCDEFGHI-policy",
|
||||
Status: "enabled",
|
||||
MemberOf: []string{"group1", "group2"},
|
||||
},
|
||||
"ZBCDEFGHI": madmin.UserInfo{
|
||||
SecretKey: "",
|
||||
PolicyName: "ZBCDEFGHI-policy",
|
||||
Status: "enabled",
|
||||
MemberOf: []string{"group1", "group2"},
|
||||
},
|
||||
}
|
||||
|
||||
// mock function response from listUsersWithContext(ctx)
|
||||
minioListUsersMock = func() (map[string]madmin.UserInfo, error) {
|
||||
return mockUserMap, nil
|
||||
}
|
||||
// get list users response this response should have Name, CreationDate, Size and Access
|
||||
// as part of of each user
|
||||
function := "listUsers()"
|
||||
userMap, err := listUsers(adminClient)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// verify length of users is correct
|
||||
assert.Equal(len(mockUserMap), len(userMap), fmt.Sprintf("Failed on %s: length of user's lists is not the same", function))
|
||||
|
||||
for _, b := range userMap {
|
||||
assert.Contains(mockUserMap, b.AccessKey)
|
||||
assert.Equal(string(mockUserMap[b.AccessKey].Status), b.Status)
|
||||
assert.Equal(mockUserMap[b.AccessKey].PolicyName, b.Policy)
|
||||
assert.ElementsMatch(mockUserMap[b.AccessKey].MemberOf, []string{"group1", "group2"})
|
||||
}
|
||||
|
||||
// Test-2 : listUsers() Return and see that the error is handled correctly and returned
|
||||
minioListUsersMock = func() (map[string]madmin.UserInfo, error) {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
_, err = listUsers(adminClient)
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddUser(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
|
||||
// Test-1: valid case of adding a user with a proper access key
|
||||
accessKey := "ABCDEFGHI"
|
||||
secretKey := "ABCDEFGHIABCDEFGHI"
|
||||
|
||||
// mock function response from addUser() return no error
|
||||
minioAddUserMock = func(accessKey, secretKey string) error {
|
||||
return nil
|
||||
}
|
||||
// adds a valid user to MinIO
|
||||
function := "addUser()"
|
||||
user, err := addUser(adminClient, &accessKey, &secretKey)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// no error should have been returned
|
||||
assert.Nil(err, "Error is not null")
|
||||
// the same access key should be in the model users
|
||||
assert.Equal(user.AccessKey, accessKey)
|
||||
// Test-1: valid case
|
||||
accessKey = "AB"
|
||||
secretKey = "ABCDEFGHIABCDEFGHI"
|
||||
|
||||
// mock function response from addUser() return no error
|
||||
minioAddUserMock = func(accessKey, secretKey string) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
|
||||
user, err = addUser(adminClient, &accessKey, &secretKey)
|
||||
|
||||
// no error should have been returned
|
||||
assert.Nil(user, "User is not null")
|
||||
assert.NotNil(err, "An error should have been returned")
|
||||
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestListGroups(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : listGroups() Get response from minio client with two Groups and return the same number on listGroups()
|
||||
mockGroupsList := []string{"group1", "group2"}
|
||||
|
||||
// mock function response from listGroups()
|
||||
minioListGroupsMock = func() ([]string, error) {
|
||||
return mockGroupsList, nil
|
||||
}
|
||||
// get list Groups response this response should have Name, CreationDate, Size and Access
|
||||
// as part of of each Groups
|
||||
function := "listGroups()"
|
||||
groupsList, err := listGroups(ctx, adminClient)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// verify length of Groupss is correct
|
||||
assert.Equal(len(mockGroupsList), len(*groupsList), fmt.Sprintf("Failed on %s: length of Groups's lists is not the same", function))
|
||||
|
||||
for i, g := range *groupsList {
|
||||
assert.Equal(mockGroupsList[i], g)
|
||||
}
|
||||
|
||||
// Test-2 : listGroups() Return error and see that the error is handled correctly and returned
|
||||
minioListGroupsMock = func() ([]string, error) {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
_, err = listGroups(ctx, adminClient)
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddGroup(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : addGroup() add a new group with two members
|
||||
newGroup := "acmeGroup"
|
||||
groupMembers := []string{"user1", "user2"}
|
||||
// mock function response from updateGroupMembers()
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
function := "addGroup()"
|
||||
if err := addGroup(ctx, adminClient, newGroup, groupMembers); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
// Test-2 : addGroup() Return error and see that the error is handled correctly and returned
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
|
||||
if err := addGroup(ctx, adminClient, newGroup, groupMembers); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveGroup(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : removeGroup() remove group assume it has no members
|
||||
groupToRemove := "acmeGroup"
|
||||
// mock function response from updateGroupMembers()
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
function := "removeGroup()"
|
||||
if err := removeGroup(ctx, adminClient, groupToRemove); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
// Test-2 : removeGroup() Return error and see that the error is handled correctly and returned
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
if err := removeGroup(ctx, adminClient, groupToRemove); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupInfo(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : groupInfo() get group info
|
||||
groupName := "acmeGroup"
|
||||
mockResponse := &madmin.GroupDesc{
|
||||
Name: groupName,
|
||||
Policy: "policyTest",
|
||||
Members: []string{"user1", "user2"},
|
||||
Status: "enabled",
|
||||
}
|
||||
// mock function response from updateGroupMembers()
|
||||
minioGetGroupDescriptionMock = func(group string) (*madmin.GroupDesc, error) {
|
||||
return mockResponse, nil
|
||||
}
|
||||
function := "groupInfo()"
|
||||
info, err := groupInfo(ctx, adminClient, groupName)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
assert.Equal(groupName, info.Name)
|
||||
assert.Equal("policyTest", info.Policy)
|
||||
assert.ElementsMatch([]string{"user1", "user2"}, info.Members)
|
||||
assert.Equal("enabled", info.Status)
|
||||
|
||||
// Test-2 : groupInfo() Return error and see that the error is handled correctly and returned
|
||||
minioGetGroupDescriptionMock = func(group string) (*madmin.GroupDesc, error) {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
_, err = groupInfo(ctx, adminClient, groupName)
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateGroupInfo(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : addOrDeleteMembers() update group members add user3 and delete user2
|
||||
function := "addOrDeleteMembers()"
|
||||
groupName := "acmeGroup"
|
||||
mockGroupDesc := &madmin.GroupDesc{
|
||||
Name: groupName,
|
||||
Policy: "policyTest",
|
||||
Members: []string{"user1", "user2"},
|
||||
Status: "enabled",
|
||||
}
|
||||
membersDesired := []string{"user3", "user1"}
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
// Test-2 : addOrDeleteMembers() handle error correctly
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
|
||||
// Test-3 : addOrDeleteMembers() only add members but handle error on adding
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
membersDesired = []string{"user3", "user1", "user2"}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
|
||||
// Test-4: addOrDeleteMembers() no updates needed so error shall not be triggered or handled.
|
||||
minioUpdateGroupMembersMock = func(madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
membersDesired = []string{"user1", "user2"}
|
||||
if err := addOrDeleteMembers(ctx, adminClient, mockGroupDesc, membersDesired); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetGroupStatus(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
function := "setGroupStatus()"
|
||||
groupName := "acmeGroup"
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1: setGroupStatus() update valid disabled status
|
||||
expectedStatus := "disabled"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// Test-2: setGroupStatus() update valid enabled status
|
||||
expectedStatus = "enabled"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// Test-3: setGroupStatus() update invalid status, should send error
|
||||
expectedStatus = "invalid"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); assert.Error(err) {
|
||||
assert.Equal("status not valid", err.Error())
|
||||
}
|
||||
// Test-4: setGroupStatus() handler error correctly
|
||||
expectedStatus = "enabled"
|
||||
minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
if err := setGroupStatus(ctx, adminClient, groupName, expectedStatus); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,8 @@ func configureAPI(api *operations.McsAPI) http.Handler {
|
||||
registerProfilingHandler(api)
|
||||
// Register session handlers
|
||||
registerSessionHandlers(api)
|
||||
// Register admin info handlers
|
||||
registerAdminInfoHandlers(api)
|
||||
|
||||
api.PreServerShutdown = func() {}
|
||||
|
||||
|
||||
@@ -50,6 +50,29 @@ func init() {
|
||||
"version": "0.1.0"
|
||||
},
|
||||
"paths": {
|
||||
"/api/v1/admin/info": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"AdminAPI"
|
||||
],
|
||||
"summary": "Returns information about the deployment",
|
||||
"operationId": "AdminInfo",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "A successful response.",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/adminInfoResponse"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "Generic error response.",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/error"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/buckets": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -990,6 +1013,20 @@ func init() {
|
||||
}
|
||||
}
|
||||
},
|
||||
"adminInfoResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"buckets": {
|
||||
"type": "integer"
|
||||
},
|
||||
"objects": {
|
||||
"type": "integer"
|
||||
},
|
||||
"usage": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"bucket": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
@@ -1516,6 +1553,29 @@ func init() {
|
||||
"version": "0.1.0"
|
||||
},
|
||||
"paths": {
|
||||
"/api/v1/admin/info": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"AdminAPI"
|
||||
],
|
||||
"summary": "Returns information about the deployment",
|
||||
"operationId": "AdminInfo",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "A successful response.",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/adminInfoResponse"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "Generic error response.",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/error"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/buckets": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -2456,6 +2516,20 @@ func init() {
|
||||
}
|
||||
}
|
||||
},
|
||||
"adminInfoResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"buckets": {
|
||||
"type": "integer"
|
||||
},
|
||||
"objects": {
|
||||
"type": "integer"
|
||||
},
|
||||
"usage": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"bucket": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
|
||||
90
restapi/operations/admin_api/admin_info.go
Normal file
90
restapi/operations/admin_api/admin_info.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// Code generated by go-swagger; DO NOT EDIT.
|
||||
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package admin_api
|
||||
|
||||
// This file was generated by the swagger tool.
|
||||
// Editing this file might prove futile when you re-run the generate command
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-openapi/runtime/middleware"
|
||||
|
||||
"github.com/minio/m3/mcs/models"
|
||||
)
|
||||
|
||||
// AdminInfoHandlerFunc turns a function with the right signature into a admin info handler
|
||||
type AdminInfoHandlerFunc func(AdminInfoParams, *models.Principal) middleware.Responder
|
||||
|
||||
// Handle executing the request and returning a response
|
||||
func (fn AdminInfoHandlerFunc) Handle(params AdminInfoParams, principal *models.Principal) middleware.Responder {
|
||||
return fn(params, principal)
|
||||
}
|
||||
|
||||
// AdminInfoHandler interface for that can handle valid admin info params
|
||||
type AdminInfoHandler interface {
|
||||
Handle(AdminInfoParams, *models.Principal) middleware.Responder
|
||||
}
|
||||
|
||||
// NewAdminInfo creates a new http.Handler for the admin info operation
|
||||
func NewAdminInfo(ctx *middleware.Context, handler AdminInfoHandler) *AdminInfo {
|
||||
return &AdminInfo{Context: ctx, Handler: handler}
|
||||
}
|
||||
|
||||
/*AdminInfo swagger:route GET /api/v1/admin/info AdminAPI adminInfo
|
||||
|
||||
Returns information about the deployment
|
||||
|
||||
*/
|
||||
type AdminInfo struct {
|
||||
Context *middleware.Context
|
||||
Handler AdminInfoHandler
|
||||
}
|
||||
|
||||
func (o *AdminInfo) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
route, rCtx, _ := o.Context.RouteInfo(r)
|
||||
if rCtx != nil {
|
||||
r = rCtx
|
||||
}
|
||||
var Params = NewAdminInfoParams()
|
||||
|
||||
uprinc, aCtx, err := o.Context.Authorize(r, route)
|
||||
if err != nil {
|
||||
o.Context.Respond(rw, r, route.Produces, route, err)
|
||||
return
|
||||
}
|
||||
if aCtx != nil {
|
||||
r = aCtx
|
||||
}
|
||||
var principal *models.Principal
|
||||
if uprinc != nil {
|
||||
principal = uprinc.(*models.Principal) // this is really a models.Principal, I promise
|
||||
}
|
||||
|
||||
if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params
|
||||
o.Context.Respond(rw, r, route.Produces, route, err)
|
||||
return
|
||||
}
|
||||
|
||||
res := o.Handler.Handle(Params, principal) // actually handle the request
|
||||
|
||||
o.Context.Respond(rw, r, route.Produces, route, res)
|
||||
|
||||
}
|
||||
62
restapi/operations/admin_api/admin_info_parameters.go
Normal file
62
restapi/operations/admin_api/admin_info_parameters.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// Code generated by go-swagger; DO NOT EDIT.
|
||||
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package admin_api
|
||||
|
||||
// This file was generated by the swagger tool.
|
||||
// Editing this file might prove futile when you re-run the swagger generate command
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-openapi/errors"
|
||||
"github.com/go-openapi/runtime/middleware"
|
||||
)
|
||||
|
||||
// NewAdminInfoParams creates a new AdminInfoParams object
|
||||
// no default values defined in spec.
|
||||
func NewAdminInfoParams() AdminInfoParams {
|
||||
|
||||
return AdminInfoParams{}
|
||||
}
|
||||
|
||||
// AdminInfoParams contains all the bound params for the admin info operation
|
||||
// typically these are obtained from a http.Request
|
||||
//
|
||||
// swagger:parameters AdminInfo
|
||||
type AdminInfoParams struct {
|
||||
|
||||
// HTTP Request Object
|
||||
HTTPRequest *http.Request `json:"-"`
|
||||
}
|
||||
|
||||
// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
|
||||
// for simple values it will use straight method calls.
|
||||
//
|
||||
// To ensure default values, the struct must have been initialized with NewAdminInfoParams() beforehand.
|
||||
func (o *AdminInfoParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
|
||||
var res []error
|
||||
|
||||
o.HTTPRequest = r
|
||||
|
||||
if len(res) > 0 {
|
||||
return errors.CompositeValidationError(res...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
133
restapi/operations/admin_api/admin_info_responses.go
Normal file
133
restapi/operations/admin_api/admin_info_responses.go
Normal file
@@ -0,0 +1,133 @@
|
||||
// Code generated by go-swagger; DO NOT EDIT.
|
||||
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package admin_api
|
||||
|
||||
// This file was generated by the swagger tool.
|
||||
// Editing this file might prove futile when you re-run the swagger generate command
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-openapi/runtime"
|
||||
|
||||
"github.com/minio/m3/mcs/models"
|
||||
)
|
||||
|
||||
// AdminInfoOKCode is the HTTP code returned for type AdminInfoOK
|
||||
const AdminInfoOKCode int = 200
|
||||
|
||||
/*AdminInfoOK A successful response.
|
||||
|
||||
swagger:response adminInfoOK
|
||||
*/
|
||||
type AdminInfoOK struct {
|
||||
|
||||
/*
|
||||
In: Body
|
||||
*/
|
||||
Payload *models.AdminInfoResponse `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
// NewAdminInfoOK creates AdminInfoOK with default headers values
|
||||
func NewAdminInfoOK() *AdminInfoOK {
|
||||
|
||||
return &AdminInfoOK{}
|
||||
}
|
||||
|
||||
// WithPayload adds the payload to the admin info o k response
|
||||
func (o *AdminInfoOK) WithPayload(payload *models.AdminInfoResponse) *AdminInfoOK {
|
||||
o.Payload = payload
|
||||
return o
|
||||
}
|
||||
|
||||
// SetPayload sets the payload to the admin info o k response
|
||||
func (o *AdminInfoOK) SetPayload(payload *models.AdminInfoResponse) {
|
||||
o.Payload = payload
|
||||
}
|
||||
|
||||
// WriteResponse to the client
|
||||
func (o *AdminInfoOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
|
||||
|
||||
rw.WriteHeader(200)
|
||||
if o.Payload != nil {
|
||||
payload := o.Payload
|
||||
if err := producer.Produce(rw, payload); err != nil {
|
||||
panic(err) // let the recovery middleware deal with this
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*AdminInfoDefault Generic error response.
|
||||
|
||||
swagger:response adminInfoDefault
|
||||
*/
|
||||
type AdminInfoDefault struct {
|
||||
_statusCode int
|
||||
|
||||
/*
|
||||
In: Body
|
||||
*/
|
||||
Payload *models.Error `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
// NewAdminInfoDefault creates AdminInfoDefault with default headers values
|
||||
func NewAdminInfoDefault(code int) *AdminInfoDefault {
|
||||
if code <= 0 {
|
||||
code = 500
|
||||
}
|
||||
|
||||
return &AdminInfoDefault{
|
||||
_statusCode: code,
|
||||
}
|
||||
}
|
||||
|
||||
// WithStatusCode adds the status to the admin info default response
|
||||
func (o *AdminInfoDefault) WithStatusCode(code int) *AdminInfoDefault {
|
||||
o._statusCode = code
|
||||
return o
|
||||
}
|
||||
|
||||
// SetStatusCode sets the status to the admin info default response
|
||||
func (o *AdminInfoDefault) SetStatusCode(code int) {
|
||||
o._statusCode = code
|
||||
}
|
||||
|
||||
// WithPayload adds the payload to the admin info default response
|
||||
func (o *AdminInfoDefault) WithPayload(payload *models.Error) *AdminInfoDefault {
|
||||
o.Payload = payload
|
||||
return o
|
||||
}
|
||||
|
||||
// SetPayload sets the payload to the admin info default response
|
||||
func (o *AdminInfoDefault) SetPayload(payload *models.Error) {
|
||||
o.Payload = payload
|
||||
}
|
||||
|
||||
// WriteResponse to the client
|
||||
func (o *AdminInfoDefault) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
|
||||
|
||||
rw.WriteHeader(o._statusCode)
|
||||
if o.Payload != nil {
|
||||
payload := o.Payload
|
||||
if err := producer.Produce(rw, payload); err != nil {
|
||||
panic(err) // let the recovery middleware deal with this
|
||||
}
|
||||
}
|
||||
}
|
||||
101
restapi/operations/admin_api/admin_info_urlbuilder.go
Normal file
101
restapi/operations/admin_api/admin_info_urlbuilder.go
Normal file
@@ -0,0 +1,101 @@
|
||||
// Code generated by go-swagger; DO NOT EDIT.
|
||||
|
||||
// This file is part of MinIO Console Server
|
||||
// Copyright (c) 2020 MinIO, Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package admin_api
|
||||
|
||||
// This file was generated by the swagger tool.
|
||||
// Editing this file might prove futile when you re-run the generate command
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
golangswaggerpaths "path"
|
||||
)
|
||||
|
||||
// AdminInfoURL generates an URL for the admin info operation
|
||||
type AdminInfoURL struct {
|
||||
_basePath string
|
||||
}
|
||||
|
||||
// WithBasePath sets the base path for this url builder, only required when it's different from the
|
||||
// base path specified in the swagger spec.
|
||||
// When the value of the base path is an empty string
|
||||
func (o *AdminInfoURL) WithBasePath(bp string) *AdminInfoURL {
|
||||
o.SetBasePath(bp)
|
||||
return o
|
||||
}
|
||||
|
||||
// SetBasePath sets the base path for this url builder, only required when it's different from the
|
||||
// base path specified in the swagger spec.
|
||||
// When the value of the base path is an empty string
|
||||
func (o *AdminInfoURL) SetBasePath(bp string) {
|
||||
o._basePath = bp
|
||||
}
|
||||
|
||||
// Build a url path and query string
|
||||
func (o *AdminInfoURL) Build() (*url.URL, error) {
|
||||
var _result url.URL
|
||||
|
||||
var _path = "/api/v1/admin/info"
|
||||
|
||||
_basePath := o._basePath
|
||||
_result.Path = golangswaggerpaths.Join(_basePath, _path)
|
||||
|
||||
return &_result, nil
|
||||
}
|
||||
|
||||
// Must is a helper function to panic when the url builder returns an error
|
||||
func (o *AdminInfoURL) Must(u *url.URL, err error) *url.URL {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if u == nil {
|
||||
panic("url can't be nil")
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// String returns the string representation of the path with query string
|
||||
func (o *AdminInfoURL) String() string {
|
||||
return o.Must(o.Build()).String()
|
||||
}
|
||||
|
||||
// BuildFull builds a full url with scheme, host, path and query string
|
||||
func (o *AdminInfoURL) BuildFull(scheme, host string) (*url.URL, error) {
|
||||
if scheme == "" {
|
||||
return nil, errors.New("scheme is required for a full url on AdminInfoURL")
|
||||
}
|
||||
if host == "" {
|
||||
return nil, errors.New("host is required for a full url on AdminInfoURL")
|
||||
}
|
||||
|
||||
base, err := o.Build()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base.Scheme = scheme
|
||||
base.Host = host
|
||||
return base, nil
|
||||
}
|
||||
|
||||
// StringFull returns the string representation of a complete url
|
||||
func (o *AdminInfoURL) StringFull(scheme, host string) string {
|
||||
return o.Must(o.BuildFull(scheme, host)).String()
|
||||
}
|
||||
@@ -72,6 +72,9 @@ func NewMcsAPI(spec *loads.Document) *McsAPI {
|
||||
AdminAPIAddUserHandler: admin_api.AddUserHandlerFunc(func(params admin_api.AddUserParams, principal *models.Principal) middleware.Responder {
|
||||
return middleware.NotImplemented("operation admin_api.AddUser has not yet been implemented")
|
||||
}),
|
||||
AdminAPIAdminInfoHandler: admin_api.AdminInfoHandlerFunc(func(params admin_api.AdminInfoParams, principal *models.Principal) middleware.Responder {
|
||||
return middleware.NotImplemented("operation admin_api.AdminInfo has not yet been implemented")
|
||||
}),
|
||||
UserAPIBucketInfoHandler: user_api.BucketInfoHandlerFunc(func(params user_api.BucketInfoParams, principal *models.Principal) middleware.Responder {
|
||||
return middleware.NotImplemented("operation user_api.BucketInfo has not yet been implemented")
|
||||
}),
|
||||
@@ -202,6 +205,8 @@ type McsAPI struct {
|
||||
AdminAPIAddPolicyHandler admin_api.AddPolicyHandler
|
||||
// AdminAPIAddUserHandler sets the operation handler for the add user operation
|
||||
AdminAPIAddUserHandler admin_api.AddUserHandler
|
||||
// AdminAPIAdminInfoHandler sets the operation handler for the admin info operation
|
||||
AdminAPIAdminInfoHandler admin_api.AdminInfoHandler
|
||||
// UserAPIBucketInfoHandler sets the operation handler for the bucket info operation
|
||||
UserAPIBucketInfoHandler user_api.BucketInfoHandler
|
||||
// UserAPIBucketSetPolicyHandler sets the operation handler for the bucket set policy operation
|
||||
@@ -334,6 +339,9 @@ func (o *McsAPI) Validate() error {
|
||||
if o.AdminAPIAddUserHandler == nil {
|
||||
unregistered = append(unregistered, "admin_api.AddUserHandler")
|
||||
}
|
||||
if o.AdminAPIAdminInfoHandler == nil {
|
||||
unregistered = append(unregistered, "admin_api.AdminInfoHandler")
|
||||
}
|
||||
if o.UserAPIBucketInfoHandler == nil {
|
||||
unregistered = append(unregistered, "user_api.BucketInfoHandler")
|
||||
}
|
||||
@@ -524,6 +532,10 @@ func (o *McsAPI) initHandlerCache() {
|
||||
if o.handlers["GET"] == nil {
|
||||
o.handlers["GET"] = make(map[string]http.Handler)
|
||||
}
|
||||
o.handlers["GET"]["/api/v1/admin/info"] = admin_api.NewAdminInfo(o.context, o.AdminAPIAdminInfoHandler)
|
||||
if o.handlers["GET"] == nil {
|
||||
o.handlers["GET"] = make(map[string]http.Handler)
|
||||
}
|
||||
o.handlers["GET"]["/api/v1/buckets/{name}"] = user_api.NewBucketInfo(o.context, o.UserAPIBucketInfoHandler)
|
||||
if o.handlers["PUT"] == nil {
|
||||
o.handlers["PUT"] = make(map[string]http.Handler)
|
||||
|
||||
24
swagger.yml
24
swagger.yml
@@ -611,6 +611,21 @@ paths:
|
||||
$ref: "#/definitions/error"
|
||||
tags:
|
||||
- UserAPI
|
||||
/api/v1/admin/info:
|
||||
get:
|
||||
summary: Returns information about the deployment
|
||||
operationId: AdminInfo
|
||||
responses:
|
||||
200:
|
||||
description: A successful response.
|
||||
schema:
|
||||
$ref: "#/definitions/adminInfoResponse"
|
||||
default:
|
||||
description: Generic error response.
|
||||
schema:
|
||||
$ref: "#/definitions/error"
|
||||
tags:
|
||||
- AdminAPI
|
||||
definitions:
|
||||
bucketAccess:
|
||||
type: string
|
||||
@@ -975,3 +990,12 @@ definitions:
|
||||
status:
|
||||
type: string
|
||||
enum: [ok]
|
||||
adminInfoResponse:
|
||||
type: object
|
||||
properties:
|
||||
buckets:
|
||||
type: integer
|
||||
objects:
|
||||
type: integer
|
||||
usage:
|
||||
type: integer
|
||||
|
||||
Reference in New Issue
Block a user