|
| 1 | +package certificate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "github.com/argoproj/argo-cd/v2/pkg/apiclient/certificate" |
| 7 | + "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" |
| 8 | + "github.com/devtron-labs/devtron/client/argocdServer/connection" |
| 9 | + "github.com/devtron-labs/devtron/util/argo" |
| 10 | + "go.uber.org/zap" |
| 11 | + "google.golang.org/grpc" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type Client interface { |
| 16 | + ListCertificates(ctx context.Context, query *certificate.RepositoryCertificateQuery, opts ...grpc.CallOption) (*v1alpha1.RepositoryCertificateList, error) |
| 17 | + CreateCertificate(ctx context.Context, query *certificate.RepositoryCertificateCreateRequest) (*v1alpha1.RepositoryCertificateList, error) |
| 18 | + DeleteCertificate(ctx context.Context, query *certificate.RepositoryCertificateQuery, opts ...grpc.CallOption) (*v1alpha1.RepositoryCertificateList, error) |
| 19 | +} |
| 20 | + |
| 21 | +type ServiceClientImpl struct { |
| 22 | + logger *zap.SugaredLogger |
| 23 | + argoCDConnectionManager connection.ArgoCDConnectionManager |
| 24 | + argoUserService argo.ArgoUserService |
| 25 | +} |
| 26 | + |
| 27 | +func NewServiceClientImpl( |
| 28 | + logger *zap.SugaredLogger, |
| 29 | + argoCDConnectionManager connection.ArgoCDConnectionManager, |
| 30 | + argoUserService argo.ArgoUserService) *ServiceClientImpl { |
| 31 | + return &ServiceClientImpl{ |
| 32 | + logger: logger, |
| 33 | + argoCDConnectionManager: argoCDConnectionManager, |
| 34 | + argoUserService: argoUserService, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (c *ServiceClientImpl) getService(ctx context.Context) (certificate.CertificateServiceClient, error) { |
| 39 | + token, ok := ctx.Value("token").(string) |
| 40 | + if !ok { |
| 41 | + return nil, errors.New("Unauthorized") |
| 42 | + } |
| 43 | + conn := c.argoCDConnectionManager.GetConnection(token) |
| 44 | + //defer conn.Close() |
| 45 | + return certificate.NewCertificateServiceClient(conn), nil |
| 46 | +} |
| 47 | + |
| 48 | +func (c *ServiceClientImpl) ListCertificates(ctx context.Context, query *certificate.RepositoryCertificateQuery, opts ...grpc.CallOption) (*v1alpha1.RepositoryCertificateList, error) { |
| 49 | + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 50 | + defer cancel() |
| 51 | + client, err := c.getService(ctx) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + return client.ListCertificates(ctx, query) |
| 56 | +} |
| 57 | + |
| 58 | +func (c *ServiceClientImpl) CreateCertificate(ctx context.Context, query *certificate.RepositoryCertificateCreateRequest) (*v1alpha1.RepositoryCertificateList, error) { |
| 59 | + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 60 | + defer cancel() |
| 61 | + client, err := c.getService(ctx) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + return client.CreateCertificate(ctx, query) |
| 66 | +} |
| 67 | + |
| 68 | +func (c *ServiceClientImpl) DeleteCertificate(ctx context.Context, query *certificate.RepositoryCertificateQuery, opts ...grpc.CallOption) (*v1alpha1.RepositoryCertificateList, error) { |
| 69 | + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 70 | + defer cancel() |
| 71 | + client, err := c.getService(ctx) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + return client.DeleteCertificate(ctx, query, opts...) |
| 76 | +} |
0 commit comments