github.com/argoproj/argo-cd/v3@v3.2.1/server/notification/notification.go (about)

     1  package notification
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/argoproj/notifications-engine/pkg/api"
     7  	apierrors "k8s.io/apimachinery/pkg/api/errors"
     8  	"k8s.io/utils/ptr"
     9  
    10  	"github.com/argoproj/argo-cd/v3/pkg/apiclient/notification"
    11  )
    12  
    13  // Server provides an Application service
    14  type Server struct {
    15  	apiFactory api.Factory
    16  }
    17  
    18  // NewServer returns a new instance of the Application service
    19  func NewServer(apiFactory api.Factory) notification.NotificationServiceServer {
    20  	s := &Server{apiFactory: apiFactory}
    21  	return s
    22  }
    23  
    24  // List returns list of notification triggers
    25  func (s *Server) ListTriggers(_ context.Context, _ *notification.TriggersListRequest) (*notification.TriggerList, error) {
    26  	api, err := s.apiFactory.GetAPI()
    27  	if err != nil {
    28  		if apierrors.IsNotFound(err) {
    29  			return &notification.TriggerList{}, nil
    30  		}
    31  	}
    32  	triggers := []*notification.Trigger{}
    33  	for trigger := range api.GetConfig().Triggers {
    34  		triggers = append(triggers, &notification.Trigger{Name: ptr.To(trigger)})
    35  	}
    36  	return &notification.TriggerList{Items: triggers}, nil
    37  }
    38  
    39  // List returns list of notification services
    40  func (s *Server) ListServices(_ context.Context, _ *notification.ServicesListRequest) (*notification.ServiceList, error) {
    41  	api, err := s.apiFactory.GetAPI()
    42  	if err != nil {
    43  		if apierrors.IsNotFound(err) {
    44  			return &notification.ServiceList{}, nil
    45  		}
    46  		return nil, err
    47  	}
    48  	services := []*notification.Service{}
    49  	for svc := range api.GetConfig().Services {
    50  		services = append(services, &notification.Service{Name: ptr.To(svc)})
    51  	}
    52  	return &notification.ServiceList{Items: services}, nil
    53  }
    54  
    55  // List returns list of notification templates
    56  func (s *Server) ListTemplates(_ context.Context, _ *notification.TemplatesListRequest) (*notification.TemplateList, error) {
    57  	api, err := s.apiFactory.GetAPI()
    58  	if err != nil {
    59  		if apierrors.IsNotFound(err) {
    60  			return &notification.TemplateList{}, nil
    61  		}
    62  		return nil, err
    63  	}
    64  	templates := []*notification.Template{}
    65  	for tmpl := range api.GetConfig().Templates {
    66  		templates = append(templates, &notification.Template{Name: ptr.To(tmpl)})
    67  	}
    68  	return &notification.TemplateList{Items: templates}, nil
    69  }