github.com/minio/console@v1.4.1/api/admin_notification_endpoints.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  
    23  	"github.com/go-openapi/runtime/middleware"
    24  	"github.com/minio/console/api/operations"
    25  	configurationApi "github.com/minio/console/api/operations/configuration"
    26  	"github.com/minio/console/models"
    27  )
    28  
    29  func registerAdminNotificationEndpointsHandlers(api *operations.ConsoleAPI) {
    30  	// return a list of notification endpoints
    31  	api.ConfigurationNotificationEndpointListHandler = configurationApi.NotificationEndpointListHandlerFunc(func(params configurationApi.NotificationEndpointListParams, session *models.Principal) middleware.Responder {
    32  		notifEndpoints, err := getNotificationEndpointsResponse(session, params)
    33  		if err != nil {
    34  			return configurationApi.NewNotificationEndpointListDefault(err.Code).WithPayload(err.APIError)
    35  		}
    36  		return configurationApi.NewNotificationEndpointListOK().WithPayload(notifEndpoints)
    37  	})
    38  	// add a new notification endpoints
    39  	api.ConfigurationAddNotificationEndpointHandler = configurationApi.AddNotificationEndpointHandlerFunc(func(params configurationApi.AddNotificationEndpointParams, session *models.Principal) middleware.Responder {
    40  		notifEndpoints, err := getAddNotificationEndpointResponse(session, params)
    41  		if err != nil {
    42  			return configurationApi.NewAddNotificationEndpointDefault(err.Code).WithPayload(err.APIError)
    43  		}
    44  		return configurationApi.NewAddNotificationEndpointCreated().WithPayload(notifEndpoints)
    45  	})
    46  }
    47  
    48  // getNotificationEndpoints invokes admin info and returns a list of notification endpoints
    49  func getNotificationEndpoints(ctx context.Context, client MinioAdmin) (*models.NotifEndpointResponse, error) {
    50  	serverInfo, err := client.serverInfo(ctx)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	var listEndpoints []*models.NotificationEndpointItem
    55  	for i := range serverInfo.Services.Notifications {
    56  		for service, endpointStatus := range serverInfo.Services.Notifications[i] {
    57  			for j := range endpointStatus {
    58  				for account, status := range endpointStatus[j] {
    59  					listEndpoints = append(listEndpoints, &models.NotificationEndpointItem{
    60  						Service:   models.NofiticationService(service),
    61  						AccountID: account,
    62  						Status:    status.Status,
    63  					})
    64  				}
    65  			}
    66  		}
    67  	}
    68  
    69  	// build response
    70  	return &models.NotifEndpointResponse{
    71  		NotificationEndpoints: listEndpoints,
    72  	}, nil
    73  }
    74  
    75  // getNotificationEndpointsResponse returns a list of notification endpoints in the instance
    76  func getNotificationEndpointsResponse(session *models.Principal, params configurationApi.NotificationEndpointListParams) (*models.NotifEndpointResponse, *CodedAPIError) {
    77  	ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
    78  	defer cancel()
    79  	mAdmin, err := NewMinioAdminClient(params.HTTPRequest.Context(), session)
    80  	if err != nil {
    81  		return nil, ErrorWithContext(ctx, err)
    82  	}
    83  	// create a minioClient interface implementation
    84  	// defining the client to be used
    85  	adminClient := AdminClient{Client: mAdmin}
    86  	// serialize output
    87  	notfEndpointResp, err := getNotificationEndpoints(ctx, adminClient)
    88  	if err != nil {
    89  		return nil, ErrorWithContext(ctx, err)
    90  	}
    91  	return notfEndpointResp, nil
    92  }
    93  
    94  func addNotificationEndpoint(ctx context.Context, client MinioAdmin, params *configurationApi.AddNotificationEndpointParams) (*models.SetNotificationEndpointResponse, error) {
    95  	configs := []*models.ConfigurationKV{}
    96  	var configName string
    97  
    98  	// we have different add validations for each service
    99  	switch *params.Body.Service {
   100  	case models.NofiticationServiceAmqp:
   101  		configName = "notify_amqp"
   102  	case models.NofiticationServiceMqtt:
   103  		configName = "notify_mqtt"
   104  	case models.NofiticationServiceElasticsearch:
   105  		configName = "notify_elasticsearch"
   106  	case models.NofiticationServiceRedis:
   107  		configName = "notify_redis"
   108  	case models.NofiticationServiceNats:
   109  		configName = "notify_nats"
   110  	case models.NofiticationServicePostgres:
   111  		configName = "notify_postgres"
   112  	case models.NofiticationServiceMysql:
   113  		configName = "notify_mysql"
   114  	case models.NofiticationServiceKafka:
   115  		configName = "notify_kafka"
   116  	case models.NofiticationServiceWebhook:
   117  		configName = "notify_webhook"
   118  	case models.NofiticationServiceNsq:
   119  		configName = "notify_nsq"
   120  	default:
   121  		return nil, errors.New("provided service is not supported")
   122  	}
   123  
   124  	// set all the config values if found on the param.Body.Properties
   125  	for k, val := range params.Body.Properties {
   126  		configs = append(configs, &models.ConfigurationKV{
   127  			Key:   k,
   128  			Value: val,
   129  		})
   130  	}
   131  
   132  	needsRestart, err := setConfigWithARNAccountID(ctx, client, &configName, configs, *params.Body.AccountID)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	return &models.SetNotificationEndpointResponse{
   138  		AccountID:  params.Body.AccountID,
   139  		Properties: params.Body.Properties,
   140  		Service:    params.Body.Service,
   141  		Restart:    needsRestart,
   142  	}, nil
   143  }
   144  
   145  // getNotificationEndpointsResponse returns a list of notification endpoints in the instance
   146  func getAddNotificationEndpointResponse(session *models.Principal, params configurationApi.AddNotificationEndpointParams) (*models.SetNotificationEndpointResponse, *CodedAPIError) {
   147  	ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
   148  	defer cancel()
   149  	mAdmin, err := NewMinioAdminClient(params.HTTPRequest.Context(), session)
   150  	if err != nil {
   151  		return nil, ErrorWithContext(ctx, err)
   152  	}
   153  	// create a minioClient interface implementation
   154  	// defining the client to be used
   155  	adminClient := AdminClient{Client: mAdmin}
   156  	// serialize output
   157  	notfEndpointResp, err := addNotificationEndpoint(ctx, adminClient, &params)
   158  	if err != nil {
   159  		return nil, ErrorWithContext(ctx, err)
   160  	}
   161  	return notfEndpointResp, nil
   162  }