From 74e0272ce8532239c78ad183b966906498efce9a Mon Sep 17 00:00:00 2001 From: AlexandreGrdl Date: Fri, 15 Mar 2024 16:11:35 +0100 Subject: [PATCH] API Client Design changes --- client/account.go | 44 ++- client/api/profile.go | 276 +++++++++++++++++ client/api/request.go | 541 ++++++++++++++++++++++++++++++++++ client/api/utils.go | 58 ++++ client/client.go | 247 ++++------------ client/config.go | 52 ---- client/healthcheck.go | 148 ++++++++-- client/healthcheck_command.go | 89 ++++-- client/healthcheck_dns.go | 90 ++++-- client/healthcheck_http.go | 89 ++++-- client/healthcheck_tcp.go | 90 ++++-- client/healthcheck_tls.go | 90 ++++-- client/metrics.go | 56 ++-- client/organization.go | 81 ++++- client/result.go | 62 ++-- client/token.go | 148 +++++++++- cmd/account.go | 11 +- cmd/config.go | 138 +++++++++ cmd/healthcheck.go | 6 +- cmd/healthcheck_command.go | 4 +- cmd/healthcheck_dns.go | 4 +- cmd/healthcheck_http.go | 4 +- cmd/healthcheck_tcp.go | 4 +- cmd/healthcheck_tls.go | 4 +- cmd/login.go | 19 +- cmd/metrics.go | 2 +- cmd/result.go | 2 +- cmd/root.go | 52 +++- cmd/token.go | 8 +- 29 files changed, 1946 insertions(+), 473 deletions(-) create mode 100644 client/api/profile.go create mode 100644 client/api/request.go create mode 100644 client/api/utils.go delete mode 100644 client/config.go create mode 100644 cmd/config.go diff --git a/client/account.go b/client/account.go index 720f8f4..2157eeb 100644 --- a/client/account.go +++ b/client/account.go @@ -2,16 +2,50 @@ package client import ( "context" - "net/http" apitypes "github.com/appclacks/go-types" ) -func (c *Client) ChangeAccountPassword(ctx context.Context, payload apitypes.ChangeAccountPasswordInput) (apitypes.Response, error) { + +// ############################################################ +// ############################################################ +// Client public Account-useCases + +func (c *Client) ChangeAccountPasswordWithContext(ctx context.Context, payload apitypes.ChangeAccountPasswordInput) (apitypes.Response, error) { var result apitypes.Response - _, err := c.sendRequest(ctx, "/app/v1/password/change", http.MethodPost, payload, &result, nil, PasswordAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Response{}, formatError("ChangeAccountPasswordWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Response{}, formatError("ChangeAccountPasswordWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildChangePasswordRequest(payload) if err != nil { - return apitypes.Response{}, err + return apitypes.Response{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } + +func (c *Client) ChangeAccountPassword(payload apitypes.ChangeAccountPasswordInput) (apitypes.Response, error) { + var result apitypes.Response + + if !c.Credentials.HasAuth() { + return apitypes.Response{}, formatError("ChangeAccountPassword", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildChangePasswordRequest(payload) + if err != nil { + return apitypes.Response{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + + diff --git a/client/api/profile.go b/client/api/profile.go new file mode 100644 index 0000000..1c6d962 --- /dev/null +++ b/client/api/profile.go @@ -0,0 +1,276 @@ +package api + + +import ( + "fmt" + "net/http" +) + +type RequestURI string +type HttpMethod string +type PrefixURI string + +const API_DEFAULT_BASE_URL string = "https://api.appclacks.com" + +const ( + TOKEN_PREFIX_URI PrefixURI = "/api" + PASSWORD_PREFIX_URI PrefixURI = "/app" + NONE_PREFIX_URI PrefixURI = "" +) + +const ( + V1_HC_URI RequestURI = "/v1/healthcheck" + V1_HC_COMMAND_URI RequestURI = "/v1/healthcheck/command" + V1_HC_DNS_URI RequestURI = "/v1/healthcheck/dns" + V1_HC_HTTP_URI RequestURI = "/v1/healthcheck/http" + V1_HC_TCP_URI RequestURI = "/v1/healthcheck/tcp" + V1_HC_TLS_URI RequestURI = "/v1/healthcheck/tls" + + V1_METRICS_URI RequestURI = "/v1/metrics/healthchecks" + V1_RESULTS_URI RequestURI = "/v1/result/healthchecks" + + V1_ORGANIZATION_URI RequestURI = "/v1/organization" + V1_TOKEN_URI RequestURI = "/v1/token" + V1_PASSWORD_URI RequestURI = "/v1/password/change" + + REGISTER_URI RequestURI = "/register" + CABOUROTTE_DISCOVERY_URI RequestURI = "/cabourotte/discovery" + +) + +const ( + METH_GET HttpMethod = http.MethodGet + METH_HEAD HttpMethod = http.MethodHead + METH_POST HttpMethod = http.MethodPost + METH_PUT HttpMethod = http.MethodPut + METH_DELETE HttpMethod = http.MethodDelete +) + +type endPoint struct { + Name string + Uri RequestURI + Method HttpMethod + Suffix string +} + +func (ep *endPoint) getFullURI(authMode AuthMode, params ...any) string{ + var authPrefix string = "" + if authMode == TokenAuth { + authPrefix = string(TOKEN_PREFIX_URI) + } + if authMode == PasswordAuth { + authPrefix = string(PASSWORD_PREFIX_URI) + } + + return fmt.Sprintf(authPrefix+string(ep.Uri)+ep.Suffix, params...) +} + +var healthCheckGetList *endPoint = &endPoint{ + Name : "HealthCheckGetList", + Uri : V1_HC_URI, + Method: METH_GET, + Suffix: "", +} +var healthCheckGetOne *endPoint = &endPoint{ + Name : "HealthCheckGetOne", + Uri : V1_HC_URI, + Method: METH_GET, + Suffix: "/%s", +} +var healthCheckDeleteOne *endPoint = &endPoint{ + Name : "HealthCheckDeleteOne", + Uri : V1_HC_URI, + Method: METH_DELETE, + Suffix: "/%s", +} +var healthCheckCmdCreate *endPoint = &endPoint{ + Name : "HealthCheckCmdCreate", + Uri : V1_HC_COMMAND_URI, + Method: METH_POST, + Suffix: "", +} +var healthCheckCmdUpdate *endPoint = &endPoint{ + Name : "HealthCheckCmdUpdate", + Uri : V1_HC_COMMAND_URI, + Method : METH_PUT, + Suffix: "/%s", +} +var healthCheckDNSCreate *endPoint = &endPoint{ + Name : "HealthCheckDNSCreate", + Uri : V1_HC_DNS_URI, + Method: METH_POST, + Suffix: "", +} +var healthCheckDNSUpdate *endPoint = &endPoint{ + Name : "HealthCheckDNSUpdate", + Uri : V1_HC_DNS_URI, + Method: METH_PUT, + Suffix: "/%s", +} +var healthCheckHTTPCreate *endPoint = &endPoint{ + Name : "HealthCheckHTTPCreate", + Uri : V1_HC_HTTP_URI, + Method: METH_POST, + Suffix: "", +} +var healthCheckHTTPUpdate *endPoint = &endPoint{ + Name : "HealthCheckHTTPUpdate", + Uri : V1_HC_HTTP_URI, + Method: METH_PUT, + Suffix: "/%s", +} +var healthCheckTCPCreate *endPoint = &endPoint{ + Name : "HealthCheckTCPCreate", + Uri : V1_HC_TCP_URI, + Method: METH_POST, + Suffix: "", +} +var healthCheckTCPUpdate *endPoint = &endPoint{ + Name : "HealthCheckTCPUpdate", + Uri : V1_HC_TCP_URI, + Method: METH_PUT, + Suffix: "/%s", +} +var healthCheckTLSCreate *endPoint = &endPoint{ + Name : "HealthCheckTLSCreate", + Uri : V1_HC_TLS_URI, + Method: METH_POST, + Suffix: "", +} +var healthCheckTLSUpdate *endPoint = &endPoint{ + Name : "HealthCheckTLSUpdate", + Uri : V1_HC_TLS_URI, + Method: METH_PUT, + Suffix: "/%s", +} +var metricGetList *endPoint = &endPoint{ + Name : "MetricGetList", + Uri : V1_METRICS_URI, + Method: METH_GET, + Suffix: "", +} +var resultGetList *endPoint = &endPoint{ + Name : "ResultGetList", + Uri : V1_RESULTS_URI, + Method: METH_GET, + Suffix: "", +} +var organizationCreate *endPoint = &endPoint{ + Name : "OrganizationCreate", + Uri : REGISTER_URI, + Method: METH_POST, + Suffix: "", +} +var organizationGetOne *endPoint = &endPoint{ + Name : "OrganizationGetOne", + Uri : V1_ORGANIZATION_URI, + Method: METH_GET, + Suffix: "", +} +var tokenCreate *endPoint = &endPoint{ + Name : "TokenCreate", + Uri : V1_TOKEN_URI, + Method : METH_POST, + Suffix : "", +} +var tokenDelete *endPoint = &endPoint{ + Name : "TokenDelete", + Uri : V1_TOKEN_URI, + Method : METH_DELETE, + Suffix : "/%s", +} +var tokenGetOne *endPoint = &endPoint{ + Name : "TokenGetOne", + Uri : V1_TOKEN_URI, + Method : METH_GET, + Suffix : "/%s", +} +var tokenGetList *endPoint = &endPoint{ + Name : "TokenGetList", + Uri : V1_TOKEN_URI, + Method : METH_GET, + Suffix : "", +} +var passwordChange *endPoint = &endPoint{ + Name : "PasswordChange", + Uri : V1_PASSWORD_URI, + Method : METH_POST, + Suffix : "", +} +var cabourotteDiscovery *endPoint = &endPoint{ + Name : "CabourotteDiscovery", + Uri : CABOUROTTE_DISCOVERY_URI, + Method: METH_GET, + Suffix : "", +} + + +type profiler struct { + BaseURL string + + HealthCheckGetList *endPoint + HealthCheckGetOne *endPoint + HealthCheckDeleteOne *endPoint + + HealthCheckCmdCreate *endPoint + HealthCheckCmdUpdate *endPoint + + HealthCheckDNSCreate *endPoint + HealthCheckDNSUpdate *endPoint + + HealthCheckHTTPCreate *endPoint + HealthCheckHTTPUpdate *endPoint + + HealthCheckTCPCreate *endPoint + HealthCheckTCPUpdate *endPoint + + HealthCheckTLSCreate *endPoint + HealthCheckTLSUpdate *endPoint + + MetricGetList *endPoint + ResultGetList *endPoint + + OrganizationCreate *endPoint + OrganizationGetOne *endPoint + + TokenCreate *endPoint + TokenDelete *endPoint + TokenGetOne *endPoint + TokenGetList *endPoint + + CabourotteDiscovery *endPoint + PasswordChange *endPoint + + +} + +func CreateApiProfiler(baseURL string) (*profiler){ + var ApiProfiler profiler = profiler{ + BaseURL: baseURL, + HealthCheckGetList: healthCheckGetList, + HealthCheckGetOne: healthCheckGetOne, + HealthCheckDeleteOne: healthCheckDeleteOne, + HealthCheckCmdCreate: healthCheckCmdCreate, + HealthCheckCmdUpdate: healthCheckCmdUpdate, + HealthCheckDNSCreate: healthCheckDNSCreate, + HealthCheckDNSUpdate: healthCheckDNSUpdate, + HealthCheckHTTPCreate: healthCheckHTTPCreate, + HealthCheckHTTPUpdate: healthCheckHTTPUpdate, + HealthCheckTCPCreate: healthCheckTCPCreate, + HealthCheckTCPUpdate: healthCheckTCPUpdate, + HealthCheckTLSCreate: healthCheckTLSCreate, + HealthCheckTLSUpdate: healthCheckTLSUpdate, + MetricGetList: metricGetList, + ResultGetList: resultGetList, + OrganizationCreate: organizationCreate, + OrganizationGetOne: organizationGetOne, + TokenCreate: tokenCreate, + TokenDelete: tokenDelete, + TokenGetOne: tokenGetOne, + TokenGetList: tokenGetList, + CabourotteDiscovery: cabourotteDiscovery, + PasswordChange: passwordChange, + } + + return &ApiProfiler +} diff --git a/client/api/request.go b/client/api/request.go new file mode 100644 index 0000000..3fb37d7 --- /dev/null +++ b/client/api/request.go @@ -0,0 +1,541 @@ +package api + +import ( + "encoding/base64" + "errors" + "fmt" + "io" + "net/http" + apitypes "github.com/appclacks/go-types" +) + +// ############################################################ +// + +type AuthMode string + +const TokenAuth AuthMode = "token" +const PasswordAuth AuthMode = "password" +const NoAuth AuthMode = "none" + +var ( + ErrNotFound = errors.New("Not found") +) + +// ############################################################ +// ############################################################ +// Credentials Struct et Functions + +type Credentials struct { + Type AuthMode + Identifier string + Secret string +} + +func (c *Credentials) HasAuth() bool { + if c.Type == NoAuth { return false} + return true +} + +func (c *Credentials) Base64AuthStr() string { + authString := fmt.Sprintf("%s:%s", c.Identifier, c.Secret) + return base64.StdEncoding.EncodeToString([]byte(authString)) + +} + +func CreateCredentials(mType AuthMode, identifier string, secret string) (*Credentials){ + return &Credentials{ + Type : mType, + Identifier: identifier, + Secret: secret, + } +} +// +// ############################################################### +// ############################################################### + +// ############################################################ +// ############################################################ +// Requester Struct and Request maker functions + +func CreateRequestsHelper(credentials *Credentials, baseURL string) (*RequestsHelper){ + return &RequestsHelper{ + Credentials: credentials, + Profiler: CreateApiProfiler(baseURL), + HttpClient: &http.Client{}, + } +} + +type RequestsHelper struct { + Credentials *Credentials + Profiler *profiler + HttpClient *http.Client +} + +func (r *RequestsHelper) ConsumeRequest(request *http.Request) ([]byte, error) { + + response, err := r.HttpClient.Do(request) + + if err != nil { + return nil,err + } + b, err := io.ReadAll(response.Body) + defer response.Body.Close() + + if response.StatusCode >= 400 { + if response.StatusCode == 404 { + return nil, ErrNotFound + } + return nil, fmt.Errorf("The API returned an error: status %d\n%s", response.StatusCode, string(b)) + } + + return b, nil + +} + +func (r *RequestsHelper) BuildCreateOrganizationRequest(newOrganization apitypes.CreateOrganizationInput) (*http.Request, error){ + + OCEndPoint := r.Profiler.OrganizationCreate + fullURL := getFullURL(r.Profiler.BaseURL, OCEndPoint.getFullURI(NoAuth)) + method := string(OCEndPoint.Method) + + body, err := marshalBody(newOrganization) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildGetOrganizationRequest()(*http.Request, error){ + + OGOEndPoint := r.Profiler.OrganizationGetOne + fullURL := getFullURL(r.Profiler.BaseURL, OGOEndPoint.getFullURI(r.Credentials.Type)) + method := string(OGOEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildChangePasswordRequest(newPassword apitypes.ChangeAccountPasswordInput)(*http.Request, error){ + + CPEndPoint := r.Profiler.PasswordChange + fullURL := getFullURL(r.Profiler.BaseURL, CPEndPoint.getFullURI((r.Credentials.Type))) + method := string(CPEndPoint.Method) + + body, err := marshalBody(newPassword) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildCreateTokenRequest(newToken apitypes.CreateAPITokenInput) (*http.Request, error){ + CTEndPoint := r.Profiler.TokenCreate + fullURL := getFullURL(r.Profiler.BaseURL, CTEndPoint.getFullURI((r.Credentials.Type))) + method := string(CTEndPoint.Method) + + body, err := marshalBody(newToken) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildGetListTokenRequest() (*http.Request, error){ + GLTEndPoint := r.Profiler.TokenGetList + fullURL := getFullURL(r.Profiler.BaseURL, GLTEndPoint.getFullURI(r.Credentials.Type)) + method := string(GLTEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildGetTokenRequest(tokenID string) (*http.Request, error){ + GTEndPoint := r.Profiler.TokenGetList + fullURL := getFullURL(r.Profiler.BaseURL, GTEndPoint.getFullURI(r.Credentials.Type, tokenID)) + method := string(GTEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildDeleteTokenRequest(tokenID string) (*http.Request, error){ + DTEndPoint := r.Profiler.TokenDelete + fullURL := getFullURL(r.Profiler.BaseURL, DTEndPoint.getFullURI(r.Credentials.Type, tokenID)) + method := string(DTEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildGetMetricsRequest() (*http.Request, error){ + GMEndPoint := r.Profiler.MetricGetList + fullURL := getFullURL(r.Profiler.BaseURL, GMEndPoint.getFullURI(r.Credentials.Type)) + method := string(GMEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildGetResultsRequest(params apitypes.ListHealthchecksResultsInput) (*http.Request, error){ + GREndPoint := r.Profiler.ResultGetList + fullURL := getFullURL(r.Profiler.BaseURL, GREndPoint.getFullURI(r.Credentials.Type)) + method := string(GREndPoint.Method) + + queryParams := make(map[string]string) + queryParams["start-date"] = params.StartDate.String() + queryParams["end-date"] = params.EndDate.String() + if params.HealthcheckID != "" { + queryParams["healthcheck-id"] = params.HealthcheckID + } + if params.Page != 0 { + queryParams["page"] = fmt.Sprintf("%d", params.Page) + } + if params.Success != nil { + queryParams["success"] = "false" + if *params.Success { + queryParams["success"] = "true" + } + } + + request,err := createRequest(fullURL, method, nil, queryParams) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildGetListHealthCheckRequest() (*http.Request, error){ + GLHCEndPoint := r.Profiler.HealthCheckGetList + fullURL := getFullURL(r.Profiler.BaseURL, GLHCEndPoint.getFullURI(r.Credentials.Type)) + method := string(GLHCEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildGetHealthCheckRequest(healthCheckID string) (*http.Request, error){ + GHCEndPoint := r.Profiler.HealthCheckGetOne + fullURL := getFullURL(r.Profiler.BaseURL, GHCEndPoint.getFullURI(r.Credentials.Type, healthCheckID)) + method := string(GHCEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildDeleteHealthCheckRequest(healthCheckID string) (*http.Request, error){ + DHCEndPoint := r.Profiler.HealthCheckDeleteOne + fullURL := getFullURL(r.Profiler.BaseURL, DHCEndPoint.getFullURI(r.Credentials.Type, healthCheckID)) + method := string(DHCEndPoint.Method) + + request,err := createRequest(fullURL, method, nil, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildCabourotteDiscoveryRequest(labels string) (*http.Request, error){ + CDEndPoint := r.Profiler.CabourotteDiscovery + fullURL := getFullURL(r.Profiler.BaseURL, CDEndPoint.getFullURI(r.Credentials.Type)) + method := string(CDEndPoint.Method) + + queryParams := make(map[string]string) + if labels != ""{ + queryParams["labels"] = labels + } + + request,err := createRequest(fullURL, method, nil, queryParams) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildCreateCommandHealthCheckRequest(newHealthCheckCommand apitypes.CreateCommandHealthcheckInput) (*http.Request, error){ + CCHCEndPoint := r.Profiler.HealthCheckCmdCreate + fullURL := getFullURL(r.Profiler.BaseURL, CCHCEndPoint.getFullURI(r.Credentials.Type)) + method := string(CCHCEndPoint.Method) + + body, err := marshalBody(newHealthCheckCommand) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildUpdateCommandHealthCheckRequest(healthCheckCommand apitypes.UpdateCommandHealthcheckInput) (*http.Request, error){ + UCHCEndPoint := r.Profiler.HealthCheckCmdUpdate + fullURL := getFullURL(r.Profiler.BaseURL, UCHCEndPoint.getFullURI(r.Credentials.Type, healthCheckCommand.ID)) + method := string(UCHCEndPoint.Method) + + body, err := marshalBody(healthCheckCommand) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildCreateDNSHealthCheckRequest(newHealthCheckDNS apitypes.CreateDNSHealthcheckInput) (*http.Request, error){ + CDHCEndPoint := r.Profiler.HealthCheckDNSCreate + fullURL := getFullURL(r.Profiler.BaseURL, CDHCEndPoint.getFullURI(r.Credentials.Type)) + method := string(CDHCEndPoint.Method) + + body, err := marshalBody(newHealthCheckDNS) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildUpdateDNSHealthCheckRequest(healthCheckDNS apitypes.UpdateDNSHealthcheckInput) (*http.Request, error){ + UDHCEndPoint := r.Profiler.HealthCheckDNSUpdate + fullURL := getFullURL(r.Profiler.BaseURL, UDHCEndPoint.getFullURI(r.Credentials.Type, healthCheckDNS.ID)) + method := string(UDHCEndPoint.Method) + + body, err := marshalBody(healthCheckDNS) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildCreateHTTPHealthCheckRequest(newHealthCheckHTTP apitypes.CreateHTTPHealthcheckInput) (*http.Request, error){ + CHHCEndPoint := r.Profiler.HealthCheckHTTPCreate + fullURL := getFullURL(r.Profiler.BaseURL, CHHCEndPoint.getFullURI(r.Credentials.Type)) + method := string(CHHCEndPoint.Method) + + body, err := marshalBody(newHealthCheckHTTP) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildUpdateHTTPHealthCheckRequest(healthCheckHTTP apitypes.UpdateHTTPHealthcheckInput) (*http.Request, error){ + UHHCEndPoint := r.Profiler.HealthCheckHTTPUpdate + fullURL := getFullURL(r.Profiler.BaseURL, UHHCEndPoint.getFullURI(r.Credentials.Type, healthCheckHTTP.ID)) + method := string(UHHCEndPoint.Method) + + body, err := marshalBody(healthCheckHTTP) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildCreateTCPHealthCheckRequest(newHealthCheckTCP apitypes.CreateTCPHealthcheckInput) (*http.Request, error){ + CTHCEndPoint := r.Profiler.HealthCheckTCPCreate + fullURL := getFullURL(r.Profiler.BaseURL, CTHCEndPoint.getFullURI(r.Credentials.Type)) + method := string(CTHCEndPoint.Method) + + body, err := marshalBody(newHealthCheckTCP) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildUpdateTCPHealthCheckRequest(healthCheckTCP apitypes.UpdateTCPHealthcheckInput) (*http.Request, error){ + UTHCEndPoint := r.Profiler.HealthCheckTCPUpdate + fullURL := getFullURL(r.Profiler.BaseURL, UTHCEndPoint.getFullURI(r.Credentials.Type, healthCheckTCP.ID)) + method := string(UTHCEndPoint.Method) + + body, err := marshalBody(healthCheckTCP) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} + +func (r *RequestsHelper) BuildCreateTLSHealthCheckRequest(newHealthCheckTLS apitypes.CreateTLSHealthcheckInput) (*http.Request, error){ + CTHCEndPoint := r.Profiler.HealthCheckTLSCreate + fullURL := getFullURL(r.Profiler.BaseURL, CTHCEndPoint.getFullURI(r.Credentials.Type)) + method := string(CTHCEndPoint.Method) + + body, err := marshalBody(newHealthCheckTLS) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil + +} + +func (r *RequestsHelper) BuildUpdateTLSHealthCheckRequest(healthCheckTLS apitypes.UpdateTLSHealthcheckInput) (*http.Request, error){ + UTHCEndPoint := r.Profiler.HealthCheckTLSUpdate + fullURL := getFullURL(r.Profiler.BaseURL, UTHCEndPoint.getFullURI(r.Credentials.Type, healthCheckTLS.ID)) + method := string(UTHCEndPoint.Method) + + body, err := marshalBody(healthCheckTLS) + if err != nil { + return nil, err + } + + request,err := createRequest(fullURL, method, body, nil) + if err != nil { + return nil, err + } + + setRequestHeader(r.Credentials, request) + + return request, nil +} \ No newline at end of file diff --git a/client/api/utils.go b/client/api/utils.go new file mode 100644 index 0000000..3c53fb7 --- /dev/null +++ b/client/api/utils.go @@ -0,0 +1,58 @@ +package api +import( + "bytes" + "encoding/json" + "net/url" + "net/http" + "fmt" + "io" +) + +func rawQueryParams(queryParams map[string]string) string { + q := url.Values{} + for k, v := range queryParams { + q.Add(k,v) + } + return q.Encode() +} + +func marshalBody(body any) (*bytes.Buffer, error){ + json, err := json.Marshal(body) + if err != nil { + return nil, err + } + return bytes.NewBuffer(json), nil +} + +func setRequestHeader(credentials *Credentials, request *http.Request){ + request.Header.Add("content-type", "application/json") + if credentials.Type != NoAuth { + request.Header.Add("Authorization", fmt.Sprintf("Basic %s", credentials.Base64AuthStr())) + } +} + +func createRequest(url string, method string,body *bytes.Buffer, queryParams map[string]string) (*http.Request, error){ + + var ioBody io.Reader = nil + if body != nil { + ioBody = body + } + + request, err := http.NewRequest( + method, + url, + ioBody) + if err != nil { + return nil, err + } + if queryParams != nil { + request.URL.RawQuery = rawQueryParams(queryParams) + } + + return request, nil + +} + +func getFullURL(baseURL string, fullURI string) string { + return fmt.Sprintf("%s%s", baseURL, fullURI) +} diff --git a/client/client.go b/client/client.go index b82c46b..4dd9cdf 100644 --- a/client/client.go +++ b/client/client.go @@ -1,226 +1,107 @@ package client import ( - "bytes" - "context" - "encoding/base64" "encoding/json" - "errors" "fmt" - "io" + "github.com/appclacks/cli/client/api" "net/http" - "os" ) -type Client struct { - http *http.Client - endpoint string - orgID string - accountEmail string - token string - accountPassword string - profile string +func deserializeResult(data []byte, v any) error{ + return json.Unmarshal(data, v) } -type AuthMode string - -const TokenAuth AuthMode = "token" -const PasswordAuth AuthMode = "password" -const NoAuth AuthMode = "none" - -var ( - ErrNotFound = errors.New("Not found") -) - -type CustomAuth func(*Client) -type WithTokenOpts func(*Client) -type WithUserPasswordOpts func(*Client) - -func WithToken(opts ...WithTokenOpts) CustomAuth { - return func(config *Client) { - for _, opt := range opts { - opt(config) - } - } +func formatError(funcName string, message string) error { + return fmt.Errorf(fmt.Sprintf("AppClacks Client Error :: %s(...) :: %s", funcName, message)) } -func WithProfile(profile string) CustomAuth { - return func(config *Client) { - config.profile = profile - } -} - -func OrganizationID(orgID string) WithTokenOpts { - return func(c *Client) { - c.orgID = orgID - } -} +func sendRequestGetStruct[T any](requestsHelper *api.RequestsHelper, request *http.Request, result T) (T, error){ -func Token(token string) WithTokenOpts { - return func(c *Client) { - c.token = token + data, err := requestsHelper.ConsumeRequest(request) + if err != nil { + return result, err } + + err = deserializeResult(data, &result) + + return result, err } -func WithUserPassword(opts ...WithUserPasswordOpts) CustomAuth { - return func(config *Client) { - for _, opt := range opts { - opt(config) - } - } -} +func sendRequestGetString(requestsHelper *api.RequestsHelper, request *http.Request) (string, error){ + var result string -func AccountEmail(email string) WithUserPasswordOpts { - return func(c *Client) { - c.accountEmail = email + data, err := requestsHelper.ConsumeRequest(request) + if err != nil { + return result, err } -} -func AccountPassword(password string) WithUserPasswordOpts { - return func(c *Client) { - c.accountPassword = password - } + result = string(data) + + return result, err } -func loadEnv(client *Client) { - if os.Getenv("APPCLACKS_ORGANIZATION_ID") != "" { - client.orgID = os.Getenv("APPCLACKS_ORGANIZATION_ID") - } - if os.Getenv("APPCLACKS_TOKEN") != "" { - client.token = os.Getenv("APPCLACKS_TOKEN") - } +// ############################################################ +// ############################################################ +// Options Struct - if os.Getenv("APPCLACKS_ACCOUNT_EMAIL") != "" { - client.accountEmail = os.Getenv("APPCLACKS_ACCOUNT_EMAIL") - } +type ClientOptions struct { + baseUrl string + credentials *api.Credentials +} - if os.Getenv("APPCLACKS_ACCOUNT_PASSWORD") != "" { - client.accountPassword = os.Getenv("APPCLACKS_ACCOUNT_PASSWORD") - } +func (opts *ClientOptions) UsePasswordAuth(email string, password string) (*ClientOptions) { + opts.credentials.Type = api.PasswordAuth + opts.credentials.Identifier = email + opts.credentials.Secret = password + return opts +} - if os.Getenv("APPCLACKS_PROFILE") != "" { - client.profile = os.Getenv("APPCLACKS_PROFILE") - } +func (opts *ClientOptions) UseTokenAuth(orgID string, token string) (*ClientOptions) { + opts.credentials.Type = api.TokenAuth + opts.credentials.Identifier = orgID + opts.credentials.Secret = token + return opts +} - if os.Getenv("APPCLACKS_API_ENDPOINT") != "" { - client.endpoint = os.Getenv("APPCLACKS_API_ENDPOINT") - } +func (opts *ClientOptions) SetBaseUrl(baseUrl string) (*ClientOptions) { + opts.baseUrl = baseUrl + return opts } -func loadConfigFile(client *Client) error { - configPath, err := GetConfigFilePath() - if err != nil { - return err +func GetDefaultClientOptions() (*ClientOptions){ + return &ClientOptions{ + baseUrl: api.API_DEFAULT_BASE_URL, + credentials: api.CreateCredentials(api.NoAuth, "", ""), } - config, err := ReadConfig(configPath) - if err != nil { - return err - } - if len(config.Profiles) == 0 { - return fmt.Errorf("The configuration file %s is empty", configPath) - } - profile := client.profile - if profile == "" { - profile = config.DefaultProfile - } - if profile == "" { - return fmt.Errorf("No profile selected and no default profile specified in the configuration file %s", configPath) - } - configProfile, ok := config.Profiles[profile] - if !ok { - return fmt.Errorf("Profile %s not found in the configuration file %s", profile, configPath) - } - - client.orgID = configProfile.OrganizationID - client.token = configProfile.APIToken - return nil } -func New(endpoint string, customAuth ...CustomAuth) (*Client, error) { +// +// ############################################################ +// ############################################################ - client := &Client{ - http: &http.Client{}, - endpoint: endpoint, - } - for _, auth := range customAuth { - auth(client) - } +// ############################################################ +// ############################################################ +// Client Struct - loadEnv(client) +type Client struct { + Credentials *api.Credentials + RequestsHelper *api.RequestsHelper +} - if client.accountEmail == "" && client.accountPassword == "" && client.token == "" && client.orgID == "" { - err := loadConfigFile(client) - if err != nil { - return nil, fmt.Errorf("No authentication variables defined and error while loading the Appclacks configuration file: %w", err) - } - } +func New(clientOptions *ClientOptions) (*Client) { - return client, nil + return &Client{ + Credentials: clientOptions.credentials, + RequestsHelper: api.CreateRequestsHelper(clientOptions.credentials, clientOptions.baseUrl), + } } -func (c *Client) sendRequest(ctx context.Context, url string, method string, body any, result any, queryParams map[string]string, auth AuthMode) (*http.Response, error) { - var reqBody io.Reader - if body != nil { - json, err := json.Marshal(body) - if err != nil { - return nil, err - } - reqBody = bytes.NewBuffer(json) - } - request, err := http.NewRequestWithContext( - ctx, - method, - fmt.Sprintf("%s%s", c.endpoint, url), - reqBody) - if err != nil { - return nil, err - } - if len(queryParams) != 0 { - q := request.URL.Query() - for k, v := range queryParams { - q.Add(k, v) - } - request.URL.RawQuery = q.Encode() - } - request.Header.Add("content-type", "application/json") - if auth != NoAuth { - var authString string - if auth == TokenAuth { - authString = fmt.Sprintf("%s:%s", c.orgID, c.token) - } - if auth == PasswordAuth { - authString = fmt.Sprintf("%s:%s", c.accountEmail, c.accountPassword) - } - creds := base64.StdEncoding.EncodeToString([]byte(authString)) - request.Header.Add("Authorization", fmt.Sprintf("Basic %s", creds)) - } - response, err := c.http.Do(request) - if err != nil { - return nil, err - } - b, err := io.ReadAll(response.Body) - if err != nil { - return nil, err - } - defer response.Body.Close() - if response.StatusCode >= 400 { - if response.StatusCode == 404 { - return nil, ErrNotFound - } - return nil, fmt.Errorf("The API returned an error: status %d\n%s", response.StatusCode, string(b)) - } - if result != nil { - err = json.Unmarshal(b, result) - if err != nil { - return nil, err - } - } - return response, nil -} + +// TODO : SUPPRIMER func jsonMerge(s1 any, s2 any) (map[string]any, error) { result := make(map[string]any) str1, err := json.Marshal(s1) diff --git a/client/config.go b/client/config.go deleted file mode 100644 index c9b6040..0000000 --- a/client/config.go +++ /dev/null @@ -1,52 +0,0 @@ -package client - -import ( - "fmt" - "os" - "path" - - "gopkg.in/yaml.v3" -) - -type ConfigFileProfile struct { - OrganizationID string `yaml:"organization-id"` - APIToken string `yaml:"api-token"` -} - -type ConfigFileProfiles struct { - DefaultProfile string `yaml:"default-profile"` - Profiles map[string]ConfigFileProfile `yaml:"profiles"` -} - -func GetConfigDirPath() (string, error) { - configDir, err := os.UserConfigDir() - if err != nil { - return "", err - } - return path.Join(configDir, "appclacks"), nil - -} - -func GetConfigFilePath() (string, error) { - configDir, err := GetConfigDirPath() - if err != nil { - return "", err - } - return path.Join(configDir, "appclacks.yaml"), err -} - -func ReadConfig(path string) (ConfigFileProfiles, error) { - configFile, err := os.ReadFile(path) - if err != nil { - return ConfigFileProfiles{}, err - } - var profiles ConfigFileProfiles - err = yaml.Unmarshal(configFile, &profiles) - if err != nil { - return ConfigFileProfiles{}, fmt.Errorf("Invalid YAML file at %s: %w", path, err) - } - if profiles.Profiles == nil { - profiles.Profiles = make(map[string]ConfigFileProfile) - } - return profiles, nil -} diff --git a/client/healthcheck.go b/client/healthcheck.go index 616510f..b7bf427 100644 --- a/client/healthcheck.go +++ b/client/healthcheck.go @@ -2,48 +2,162 @@ package client import ( "context" - "fmt" - "net/http" apitypes "github.com/appclacks/go-types" ) -func (c *Client) DeleteHealthcheck(ctx context.Context, input apitypes.DeleteHealthcheckInput) (apitypes.Response, error) { +// ############################################################ +// ############################################################ +// Client public Healthcheck-useCases + +func (c *Client) DeleteHealthcheckWithContext(ctx context.Context, input apitypes.DeleteHealthcheckInput) (apitypes.Response, error) { + var result apitypes.Response + + if !c.Credentials.HasAuth() { + return apitypes.Response{}, formatError("DeleteHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Response{}, formatError("DeleteHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildDeleteHealthCheckRequest(input.ID) + if err != nil { + return apitypes.Response{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +func (c *Client) DeleteHealthcheck(input apitypes.DeleteHealthcheckInput) (apitypes.Response, error) { var result apitypes.Response - _, err := c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/%s", input.ID), http.MethodDelete, nil, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Response{}, formatError("DeleteHealthcheckWithContext", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildDeleteHealthCheckRequest(input.ID) if err != nil { return apitypes.Response{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +// ----------------------------------------------------------------------------------------------------- + +func (c *Client) GetHealthcheckWithContext(ctx context.Context, input apitypes.GetHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("GetHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("GetHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildGetHealthCheckRequest(input.Identifier) + if err != nil { + return apitypes.Healthcheck{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } -func (c *Client) GetHealthcheck(ctx context.Context, input apitypes.GetHealthcheckInput) (apitypes.Healthcheck, error) { +func (c *Client) GetHealthcheck(input apitypes.GetHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - _, err := c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/%s", input.Identifier), http.MethodGet, nil, &result, nil, TokenAuth) + + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("GetHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildGetHealthCheckRequest(input.Identifier) if err != nil { return apitypes.Healthcheck{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) } -func (c *Client) ListHealthchecks(ctx context.Context) (apitypes.ListHealthchecksOutput, error) { +// ---------------------------------------------------------------------------------------------------------- + + +func (c *Client) ListHealthchecksWithContext(ctx context.Context) (apitypes.ListHealthchecksOutput, error) { var result apitypes.ListHealthchecksOutput - _, err := c.sendRequest(ctx, "/api/v1/healthcheck", http.MethodGet, nil, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.ListHealthchecksOutput{}, formatError("ListHealthchecksWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.ListHealthchecksOutput{}, formatError("ListHealthchecksWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildGetListHealthCheckRequest() if err != nil { return apitypes.ListHealthchecksOutput{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } -func (c *Client) CabourotteDiscovery(ctx context.Context, input apitypes.CabourotteDiscoveryInput) (apitypes.CabourotteDiscoveryOutput, error) { +func (c *Client) ListHealthchecks() (apitypes.ListHealthchecksOutput, error) { + var result apitypes.ListHealthchecksOutput + + if !c.Credentials.HasAuth() { + return apitypes.ListHealthchecksOutput{}, formatError("ListHealthchecks", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildGetListHealthCheckRequest() + if err != nil { + return apitypes.ListHealthchecksOutput{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +// -------------------------------------------------------------------------------------------------------------------------- + +func (c *Client) CabourotteDiscoveryWithContext(ctx context.Context, input apitypes.CabourotteDiscoveryInput) (apitypes.CabourotteDiscoveryOutput, error) { var result apitypes.CabourotteDiscoveryOutput - queryParams := make(map[string]string) - if input.Labels != "" { - queryParams["labels"] = input.Labels + + if !c.Credentials.HasAuth() { + return apitypes.CabourotteDiscoveryOutput{}, formatError("CabourotteDiscoveryWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.CabourotteDiscoveryOutput{}, formatError("CabourotteDiscoveryWithContext", " 'nil' value for context.Context") } - _, err := c.sendRequest(ctx, "/cabourotte/discovery", http.MethodGet, nil, &result, queryParams, TokenAuth) + + request, err := c.RequestsHelper.BuildCabourotteDiscoveryRequest(input.Labels) if err != nil { return apitypes.CabourotteDiscoveryOutput{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } + +func (c *Client) CabourotteDiscovery(input apitypes.CabourotteDiscoveryInput) (apitypes.CabourotteDiscoveryOutput, error) { + var result apitypes.CabourotteDiscoveryOutput + + if !c.Credentials.HasAuth() { + return apitypes.CabourotteDiscoveryOutput{}, formatError("CabourotteDiscovery", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCabourotteDiscoveryRequest(input.Labels) + if err != nil { + return apitypes.CabourotteDiscoveryOutput{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} \ No newline at end of file diff --git a/client/healthcheck_command.go b/client/healthcheck_command.go index 532311b..ef30550 100644 --- a/client/healthcheck_command.go +++ b/client/healthcheck_command.go @@ -2,38 +2,87 @@ package client import ( "context" - "fmt" - "net/http" apitypes "github.com/appclacks/go-types" ) -func (c *Client) CreateCommandHealthcheck(ctx context.Context, input apitypes.CreateCommandHealthcheckInput) (apitypes.Healthcheck, error) { +// ############################################################ +// ############################################################ +// Client public Account-useCases + +func (c *Client) CreateCommandHealthcheckWithContext(ctx context.Context, input apitypes.CreateCommandHealthcheckInput) (apitypes.Healthcheck, error){ + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateCommandHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("CreateCommandHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildCreateCommandHealthCheckRequest(input) + if err != nil { + return apitypes.Healthcheck{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + +} + +func (c *Client) CreateCommandHealthcheck(input apitypes.CreateCommandHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - _, err := c.sendRequest(ctx, "/api/v1/healthcheck/command", http.MethodPost, input, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateCommandHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCreateCommandHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) } -func (c *Client) UpdateCommandHealthcheck(ctx context.Context, input apitypes.UpdateCommandHealthcheckInput) (apitypes.Healthcheck, error) { +// ------------------------------------------------------------------------------------------------------------------------------- + + +func (c *Client) UpdateCommandHealthcheckWithContext(ctx context.Context, input apitypes.UpdateCommandHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - internalInput := internalUpdateHealthcheckInput{ - Name: input.Name, - Description: input.Description, - Labels: input.Labels, - Interval: input.Interval, - Enabled: input.Enabled, - Timeout: input.Timeout, - } - payload, err := jsonMerge(internalInput, input.HealthcheckCommandDefinition) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateCommandHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("UpdateCommandHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildUpdateCommandHealthCheckRequest(input) if err != nil { - return result, err + return apitypes.Healthcheck{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +func (c *Client) UpdateCommandHealthcheck(input apitypes.UpdateCommandHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateCommandHealthcheck", "Credentials missing") } - _, err = c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/command/%s", input.ID), http.MethodPut, payload, &result, nil, TokenAuth) + + request, err := c.RequestsHelper.BuildUpdateCommandHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + + return sendRequestGetStruct(c.RequestsHelper, request, result) } diff --git a/client/healthcheck_dns.go b/client/healthcheck_dns.go index 57ba228..176cd91 100644 --- a/client/healthcheck_dns.go +++ b/client/healthcheck_dns.go @@ -2,21 +2,56 @@ package client import ( "context" - "fmt" - "net/http" apitypes "github.com/appclacks/go-types" ) -func (c *Client) CreateDNSHealthcheck(ctx context.Context, input apitypes.CreateDNSHealthcheckInput) (apitypes.Healthcheck, error) { +// ############################################################ +// ############################################################ +// Client public HealthcheckDNS-useCases + + +func (c *Client) CreateDNSHealthcheckWithContext(ctx context.Context, input apitypes.CreateDNSHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateDNSHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("CreateDNSHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildCreateDNSHealthCheckRequest(input) + if err != nil { + return apitypes.Healthcheck{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + +} + + +func (c *Client) CreateDNSHealthcheck(input apitypes.CreateDNSHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - _, err := c.sendRequest(ctx, "/api/v1/healthcheck/dns", http.MethodPost, input, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateDNSHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCreateDNSHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } +// -------------------------------------------------------------------------------------------------------------------- + type internalUpdateHealthcheckInput struct { Name string `json:"name"` Description string `json:"description,omitempty"` @@ -26,23 +61,38 @@ type internalUpdateHealthcheckInput struct { Timeout string `json:"timeout"` } -func (c *Client) UpdateDNSHealthcheck(ctx context.Context, input apitypes.UpdateDNSHealthcheckInput) (apitypes.Healthcheck, error) { +func (c *Client) UpdateDNSHealthcheckWithContext(ctx context.Context, input apitypes.UpdateDNSHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - internalInput := internalUpdateHealthcheckInput{ - Name: input.Name, - Description: input.Description, - Labels: input.Labels, - Interval: input.Interval, - Enabled: input.Enabled, - Timeout: input.Timeout, - } - payload, err := jsonMerge(internalInput, input.HealthcheckDNSDefinition) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateDNSHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("UpdateDNSHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildUpdateDNSHealthCheckRequest(input) if err != nil { - return result, err + return apitypes.Healthcheck{}, err } - _, err = c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/dns/%s", input.ID), http.MethodPut, payload, &result, nil, TokenAuth) + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +func (c *Client) UpdateDNSHealthcheck(input apitypes.UpdateDNSHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateDNSHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildUpdateDNSHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) } diff --git a/client/healthcheck_http.go b/client/healthcheck_http.go index 6a8a064..0d9cbe0 100644 --- a/client/healthcheck_http.go +++ b/client/healthcheck_http.go @@ -2,38 +2,87 @@ package client import ( "context" - "fmt" - "net/http" apitypes "github.com/appclacks/go-types" ) -func (c *Client) CreateHTTPHealthcheck(ctx context.Context, input apitypes.CreateHTTPHealthcheckInput) (apitypes.Healthcheck, error) { +// ############################################################ +// ############################################################ +// Client public HTTPHealthcheck-useCases + +func (c *Client) CreateHTTPHealthcheckWithContext(ctx context.Context, input apitypes.CreateHTTPHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - _, err := c.sendRequest(ctx, "/api/v1/healthcheck/http", http.MethodPost, input, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateHTTPHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("CreateHTTPHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildCreateHTTPHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } -func (c *Client) UpdateHTTPHealthcheck(ctx context.Context, input apitypes.UpdateHTTPHealthcheckInput) (apitypes.Healthcheck, error) { +func (c *Client) CreateHTTPHealthcheck(input apitypes.CreateHTTPHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - internalInput := internalUpdateHealthcheckInput{ - Name: input.Name, - Description: input.Description, - Labels: input.Labels, - Interval: input.Interval, - Enabled: input.Enabled, - Timeout: input.Timeout, - } - payload, err := jsonMerge(internalInput, input.HealthcheckHTTPDefinition) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateHTTPHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCreateHTTPHealthCheckRequest(input) if err != nil { - return result, err + return apitypes.Healthcheck{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) + +} + +// ----------------------------------------------------------------------------------------------------------------------- + + +func (c *Client) UpdateHTTPHealthcheckWithContext(ctx context.Context, input apitypes.UpdateHTTPHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateHTTPHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("UpdateHTTPHealthcheckWithContext", " 'nil' value for context.Context") } - _, err = c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/http/%s", input.ID), http.MethodPut, payload, &result, nil, TokenAuth) + + request, err := c.RequestsHelper.BuildUpdateHTTPHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } + +func (c *Client) UpdateHTTPHealthcheck(input apitypes.UpdateHTTPHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateHTTPHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildUpdateHTTPHealthCheckRequest(input) + if err != nil { + return apitypes.Healthcheck{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} \ No newline at end of file diff --git a/client/healthcheck_tcp.go b/client/healthcheck_tcp.go index 355cf10..ed3bb3e 100644 --- a/client/healthcheck_tcp.go +++ b/client/healthcheck_tcp.go @@ -2,38 +2,88 @@ package client import ( "context" - "fmt" - "net/http" apitypes "github.com/appclacks/go-types" ) -func (c *Client) CreateTCPHealthcheck(ctx context.Context, input apitypes.CreateTCPHealthcheckInput) (apitypes.Healthcheck, error) { +// ############################################################ +// ############################################################ +// Client public TCPHealthcheck-useCases + +func (c *Client) CreateTCPHealthcheckWithContext(ctx context.Context, input apitypes.CreateTCPHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateTCPHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("CreateTCPHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildCreateTCPHealthCheckRequest(input) + if err != nil { + return apitypes.Healthcheck{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + +} + +func (c *Client) CreateTCPHealthcheck(input apitypes.CreateTCPHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - _, err := c.sendRequest(ctx, "/api/v1/healthcheck/tcp", http.MethodPost, input, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateTCPHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCreateTCPHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } -func (c *Client) UpdateTCPHealthcheck(ctx context.Context, input apitypes.UpdateTCPHealthcheckInput) (apitypes.Healthcheck, error) { +// ---------------------------------------------------------------------------------------------------------------------------- + +func (c *Client) UpdateTCPHealthcheckWithContext(ctx context.Context, input apitypes.UpdateTCPHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - internalInput := internalUpdateHealthcheckInput{ - Name: input.Name, - Description: input.Description, - Labels: input.Labels, - Interval: input.Interval, - Enabled: input.Enabled, - Timeout: input.Timeout, - } - payload, err := jsonMerge(internalInput, input.HealthcheckTCPDefinition) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateTCPHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("UpdateTCPHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildUpdateTCPHealthCheckRequest(input) if err != nil { - return result, err + return apitypes.Healthcheck{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + +} + +func (c *Client) UpdateTCPHealthcheck(input apitypes.UpdateTCPHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateTCPHealthcheck", "Credentials missing") } - _, err = c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/tcp/%s", input.ID), http.MethodPut, payload, &result, nil, TokenAuth) + + request, err := c.RequestsHelper.BuildUpdateTCPHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } diff --git a/client/healthcheck_tls.go b/client/healthcheck_tls.go index 0615d78..c6255c0 100644 --- a/client/healthcheck_tls.go +++ b/client/healthcheck_tls.go @@ -2,38 +2,88 @@ package client import ( "context" - "fmt" - "net/http" + apitypes "github.com/appclacks/go-types" ) -func (c *Client) CreateTLSHealthcheck(ctx context.Context, input apitypes.CreateTLSHealthcheckInput) (apitypes.Healthcheck, error) { +// ############################################################ +// ############################################################ +// Client public TLSHealthcheck-useCases + + +func (c *Client) CreateTLSHealthcheckWithContext(ctx context.Context, input apitypes.CreateTLSHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - _, err := c.sendRequest(ctx, "/api/v1/healthcheck/tls", http.MethodPost, input, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateTLSHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("CreateTLSHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildCreateTLSHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } -func (c *Client) UpdateTLSHealthcheck(ctx context.Context, input apitypes.UpdateTLSHealthcheckInput) (apitypes.Healthcheck, error) { +func (c *Client) CreateTLSHealthcheck(input apitypes.CreateTLSHealthcheckInput) (apitypes.Healthcheck, error) { var result apitypes.Healthcheck - internalInput := internalUpdateHealthcheckInput{ - Name: input.Name, - Description: input.Description, - Labels: input.Labels, - Interval: input.Interval, - Enabled: input.Enabled, - Timeout: input.Timeout, - } - payload, err := jsonMerge(internalInput, input.HealthcheckTLSDefinition) + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("CreateTLSHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCreateTLSHealthCheckRequest(input) if err != nil { - return result, err + return apitypes.Healthcheck{}, err } - _, err = c.sendRequest(ctx, fmt.Sprintf("/api/v1/healthcheck/tls/%s", input.ID), http.MethodPut, payload, &result, nil, TokenAuth) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + +} + +// --------------------------------------------------------------------------------------------------------------------------------- + +func (c *Client) UpdateTLSHealthcheckWithContext(ctx context.Context, input apitypes.UpdateTLSHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateTLSHealthcheckWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Healthcheck{}, formatError("UpdateTLSHealthcheckWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildUpdateTLSHealthCheckRequest(input) if err != nil { - return apitypes.Healthcheck{}, err + return apitypes.Healthcheck{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } + +func (c *Client) UpdateTLSHealthcheck(input apitypes.UpdateTLSHealthcheckInput) (apitypes.Healthcheck, error) { + var result apitypes.Healthcheck + + if !c.Credentials.HasAuth() { + return apitypes.Healthcheck{}, formatError("UpdateTLSHealthcheck", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildUpdateTLSHealthCheckRequest(input) + if err != nil { + return apitypes.Healthcheck{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} \ No newline at end of file diff --git a/client/metrics.go b/client/metrics.go index c9323ea..cefa6aa 100644 --- a/client/metrics.go +++ b/client/metrics.go @@ -2,36 +2,42 @@ package client import ( "context" - "encoding/base64" - "fmt" - "io" - "net/http" + ) -func (c *Client) GetHealthchecksMetrics(ctx context.Context) (string, error) { - request, err := http.NewRequestWithContext( - ctx, - http.MethodGet, - fmt.Sprintf("%s/api/v1/metrics/healthchecks", c.endpoint), - nil) - if err != nil { - return "", err +// ############################################################ +// ############################################################ +// Client public Metrics-useCases + +func (c *Client) GetHealthchecksMetricsWithContext(ctx context.Context) (string, error) { + + if !c.Credentials.HasAuth() { + return "", formatError("GetHealthchecksMetricsWithContext", "Credentials missing") } - // TODO: mutualize code - authString := fmt.Sprintf("%s:%s", c.orgID, c.token) - creds := base64.StdEncoding.EncodeToString([]byte(authString)) - request.Header.Add("Authorization", fmt.Sprintf("Basic %s", creds)) - response, err := c.http.Do(request) - if err != nil { - return "", err + + if ctx == nil { + return "", formatError("GetHealthchecksMetricsWithContext", " 'nil' value for context.Context") } - b, err := io.ReadAll(response.Body) + + request, err := c.RequestsHelper.BuildGetMetricsRequest() if err != nil { - return "", err + return "",err } - defer response.Body.Close() - if response.StatusCode >= 400 { - return "", fmt.Errorf("The API returned an error: status %d\n%s", response.StatusCode, string(b)) + + request = request.WithContext(ctx) + + return sendRequestGetString(c.RequestsHelper, request) +} + +func (c *Client) GetHealthchecksMetrics() (string, error) { + if !c.Credentials.HasAuth() { + return "", formatError("GetHealthchecksMetrics", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildGetMetricsRequest() + if err != nil { + return "",err } - return string(b), nil + + return sendRequestGetString(c.RequestsHelper, request) } diff --git a/client/organization.go b/client/organization.go index 92c9182..1778d23 100644 --- a/client/organization.go +++ b/client/organization.go @@ -2,25 +2,90 @@ package client import ( "context" - "net/http" apitypes "github.com/appclacks/go-types" ) +// ############################################################ +// ############################################################ +// Client public Organization-useCases + + + +func (c *Client) CreateOrganizationWithContext(ctx context.Context, payload apitypes.CreateOrganizationInput) (apitypes.CreateOrganizationOutput, error) { + var result apitypes.CreateOrganizationOutput + + if !c.Credentials.HasAuth() { + return apitypes.CreateOrganizationOutput{}, formatError("CreateOrganizationWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.CreateOrganizationOutput{}, formatError("CreateOrganizationWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildCreateOrganizationRequest(payload) + if err != nil { + return apitypes.CreateOrganizationOutput{}, err + } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) + +} + func (c *Client) CreateOrganization(ctx context.Context, payload apitypes.CreateOrganizationInput) (apitypes.CreateOrganizationOutput, error) { var result apitypes.CreateOrganizationOutput - _, err := c.sendRequest(ctx, "/register", http.MethodPost, payload, &result, nil, NoAuth) + + if !c.Credentials.HasAuth() { + return apitypes.CreateOrganizationOutput{}, formatError("CreateOrganization", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCreateOrganizationRequest(payload) if err != nil { - return apitypes.CreateOrganizationOutput{}, err + return apitypes.CreateOrganizationOutput{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } -func (c *Client) GetOrganizationForAccount(ctx context.Context) (apitypes.Organization, error) { +// ------------------------------------------------------------------------------------------------------------------------ + +func (c *Client) GetOrganizationForAccountWithContext(ctx context.Context) (apitypes.Organization, error) { + var result apitypes.Organization - _, err := c.sendRequest(ctx, "/app/v1/organization", http.MethodGet, nil, &result, nil, PasswordAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Organization{}, formatError("GetOrganizationForAccountWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Organization{}, formatError("GetOrganizationForAccountWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildGetOrganizationRequest() + if err != nil { - return apitypes.Organization{}, err + return apitypes.Organization{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } + +func (c *Client) GetOrganizationForAccount() (apitypes.Organization, error) { + var result apitypes.Organization + + if !c.Credentials.HasAuth() { + return apitypes.Organization{}, formatError("GetOrganizationForAccountWithContext", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildGetOrganizationRequest() + if err != nil { + return apitypes.Organization{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} \ No newline at end of file diff --git a/client/result.go b/client/result.go index a15c730..43efa9d 100644 --- a/client/result.go +++ b/client/result.go @@ -2,49 +2,49 @@ package client import ( "context" - "encoding/json" - "fmt" - "net/http" - "strconv" apitypes "github.com/appclacks/go-types" ) -func (c *Client) ListHealthchecksResults(ctx context.Context, input apitypes.ListHealthchecksResultsInput) (apitypes.ListHealthchecksResultsOutput, error) { +// ############################################################ +// ############################################################ +// Client Results public methods + + +func (c *Client) ListHealthchecksResultsWithContext(ctx context.Context, input apitypes.ListHealthchecksResultsInput) (apitypes.ListHealthchecksResultsOutput, error) { var result apitypes.ListHealthchecksResultsOutput - queryParams := make(map[string]string) - startBytes, err := json.Marshal(input.StartDate) - if err != nil { - return apitypes.ListHealthchecksResultsOutput{}, err - } - queryParams["start-date"], err = strconv.Unquote(string(startBytes)) - if err != nil { - return apitypes.ListHealthchecksResultsOutput{}, err + + if !c.Credentials.HasAuth() { + return apitypes.ListHealthchecksResultsOutput{}, formatError("ListHealthchecksResultsWithContext", "Credentials missing") } - endBytes, err := json.Marshal(input.EndDate) - if err != nil { - return apitypes.ListHealthchecksResultsOutput{}, err + + if ctx == nil { + return apitypes.ListHealthchecksResultsOutput{}, formatError("ListHealthchecksResultsWithContext", " 'nil' value for context.Context") } - queryParams["end-date"], err = strconv.Unquote(string(endBytes)) + + request, err := c.RequestsHelper.BuildGetResultsRequest(input) if err != nil { return apitypes.ListHealthchecksResultsOutput{}, err } - if input.HealthcheckID != "" { - queryParams["healthcheck-id"] = input.HealthcheckID - } - if input.Page != 0 { - queryParams["page"] = fmt.Sprintf("%d", input.Page) - } - if input.Success != nil { - if *input.Success { - queryParams["success"] = "true" - } else { - queryParams["success"] = "false" - } + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +func (c *Client) ListHealthchecksResults(input apitypes.ListHealthchecksResultsInput) (apitypes.ListHealthchecksResultsOutput, error) { + var result apitypes.ListHealthchecksResultsOutput + + if !c.Credentials.HasAuth() { + return apitypes.ListHealthchecksResultsOutput{}, formatError("ListHealthchecksResults", "Credentials missing") } - _, err = c.sendRequest(ctx, "/api/v1/result/healthchecks", http.MethodGet, nil, &result, queryParams, TokenAuth) + + request, err := c.RequestsHelper.BuildGetResultsRequest(input) if err != nil { return apitypes.ListHealthchecksResultsOutput{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) + } + diff --git a/client/token.go b/client/token.go index dd70db5..3ecc107 100644 --- a/client/token.go +++ b/client/token.go @@ -2,44 +2,162 @@ package client import ( "context" - "fmt" - "net/http" apitypes "github.com/appclacks/go-types" ) -func (c *Client) CreateAPIToken(ctx context.Context, payload apitypes.CreateAPITokenInput) (apitypes.APIToken, error) { +// ############################################################ +// ############################################################ +// Client public Token-useCases + + +func (c *Client) CreateAPITokenWithContext(ctx context.Context, payload apitypes.CreateAPITokenInput) (apitypes.APIToken, error) { var result apitypes.APIToken - _, err := c.sendRequest(ctx, "/app/v1/token", http.MethodPost, payload, &result, nil, PasswordAuth) + + if !c.Credentials.HasAuth() { + return apitypes.APIToken{}, formatError("CreateAPITokenWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.APIToken{}, formatError("CreateAPITokenWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildCreateTokenRequest(payload) if err != nil { return apitypes.APIToken{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } -func (c *Client) ListAPITokens(ctx context.Context) (apitypes.ListAPITokensOutput, error) { +func (c *Client) CreateAPIToken(payload apitypes.CreateAPITokenInput) (apitypes.APIToken, error) { + var result apitypes.APIToken + + if !c.Credentials.HasAuth() { + return apitypes.APIToken{}, formatError("CreateAPIToken", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildCreateTokenRequest(payload) + if err != nil { + return apitypes.APIToken{}, err + } + + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +// ----------------------------------------------------------------------------- + +func (c *Client) ListAPITokensWithContext(ctx context.Context) (apitypes.ListAPITokensOutput, error) { var result apitypes.ListAPITokensOutput - _, err := c.sendRequest(ctx, "/api/v1/token", http.MethodGet, nil, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.ListAPITokensOutput{}, formatError("ListAPITokensWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.ListAPITokensOutput{}, formatError("ListAPITokensWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildGetListTokenRequest() if err != nil { - return apitypes.ListAPITokensOutput{}, err + return apitypes.ListAPITokensOutput{}, err } - return result, nil + + return sendRequestGetStruct(c.RequestsHelper, request, result) } -func (c *Client) GetAPIToken(ctx context.Context, input apitypes.GetAPITokenInput) (apitypes.APIToken, error) { + +func (c *Client) ListAPITokens() (apitypes.ListAPITokensOutput, error) { + var result apitypes.ListAPITokensOutput + + if !c.Credentials.HasAuth() { + return apitypes.ListAPITokensOutput{}, formatError("ListAPITokens", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildGetListTokenRequest() + if err != nil { + return apitypes.ListAPITokensOutput{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +// ----------------------------------------------------------------------------- + +func (c *Client) GetAPITokenWithContext(ctx context.Context, input apitypes.GetAPITokenInput) (apitypes.APIToken, error) { var result apitypes.APIToken - _, err := c.sendRequest(ctx, fmt.Sprintf("/api/v1/token/%s", input.ID), http.MethodGet, nil, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.APIToken{}, formatError("GetAPITokenWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.APIToken{}, formatError("GetAPITokenWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildGetTokenRequest(input.ID) if err != nil { return apitypes.APIToken{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} + +func (c *Client) GetAPIToken(input apitypes.GetAPITokenInput) (apitypes.APIToken, error) { + var result apitypes.APIToken + + if !c.Credentials.HasAuth() { + return apitypes.APIToken{}, formatError("GetAPITokenWithContext", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildGetTokenRequest(input.ID) + if err != nil { + return apitypes.APIToken{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) } -func (c *Client) DeleteAPIToken(ctx context.Context, input apitypes.DeleteAPITokenInput) (apitypes.Response, error) { +// ----------------------------------------------------------------------------- + + +func (c *Client) DeleteAPITokenWithContext(ctx context.Context, input apitypes.DeleteAPITokenInput) (apitypes.Response, error) { var result apitypes.Response - _, err := c.sendRequest(ctx, fmt.Sprintf("/api/v1/token/%s", input.ID), http.MethodDelete, nil, &result, nil, TokenAuth) + + if !c.Credentials.HasAuth() { + return apitypes.Response{}, formatError("DeleteAPITokenWithContext", "Credentials missing") + } + + if ctx == nil { + return apitypes.Response{}, formatError("DeleteAPITokenWithContext", " 'nil' value for context.Context") + } + + request, err := c.RequestsHelper.BuildDeleteTokenRequest(input.ID) if err != nil { return apitypes.Response{}, err } - return result, nil + + request = request.WithContext(ctx) + + return sendRequestGetStruct(c.RequestsHelper, request, result) } + +func (c *Client) DeleteAPIToken(input apitypes.DeleteAPITokenInput) (apitypes.Response, error) { + var result apitypes.Response + + if !c.Credentials.HasAuth() { + return apitypes.Response{}, formatError("DeleteAPIToken", "Credentials missing") + } + + request, err := c.RequestsHelper.BuildDeleteTokenRequest(input.ID) + if err != nil { + return apitypes.Response{}, err + } + + return sendRequestGetStruct(c.RequestsHelper, request, result) +} \ No newline at end of file diff --git a/cmd/account.go b/cmd/account.go index 567ae40..da9ca39 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -8,8 +8,6 @@ import ( "os" "strings" "syscall" - - "github.com/appclacks/cli/client" apitypes "github.com/appclacks/go-types" "github.com/cheynewallace/tabby" "github.com/spf13/cobra" @@ -39,15 +37,16 @@ func changePasswordCmd() *cobra.Command { fmt.Println("") exitIfError(err) newPassword = strings.TrimSpace(newPassword) - cliClient, err := client.New(appclacksURL, client.WithUserPassword(client.AccountEmail(email), client.AccountPassword(password))) - exitIfError(err) + + cliClient := buildClientByPassword(email, password) + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() payload := apitypes.ChangeAccountPasswordInput{ NewPassword: newPassword, } - result, err := cliClient.ChangeAccountPassword(ctx, payload) + result, err := cliClient.ChangeAccountPasswordWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) @@ -75,7 +74,7 @@ func getAccountOrganizationCmd() *cobra.Command { client := buildClient() ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.GetOrganizationForAccount(ctx) + result, err := client.GetOrganizationForAccountWithContext(ctx) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..c11dbdc --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,138 @@ +package cmd + +import ( + "fmt" + "os" + "path" + "gopkg.in/yaml.v3" +) + + + +type ConnexionInfos struct { + TypeAuth string // "token" or "password" or "noAuth" + identifier string + secret string + baseURL string + profile string +} + +func (ci *ConnexionInfos) setPasswordAuth(email string, password string) { + ci.TypeAuth = "password" + ci.identifier = email + ci.secret = password +} + +func (ci *ConnexionInfos) setTokenAuth(orgID string, token string) { + ci.TypeAuth = "token" + ci.identifier = orgID + ci.secret = token +} + +func (ci *ConnexionInfos) setBaseURL(baseURL string){ + ci.baseURL = baseURL +} + +func CreateConnInfos() (*ConnexionInfos){ + return &ConnexionInfos{ + TypeAuth: "noAuth", + identifier: "", + secret: "", + baseURL: "", + profile: "", + } +} + +type ConfigFileProfile struct { + OrganizationID string `yaml:"organization-id"` + APIToken string `yaml:"api-token"` +} + +type ConfigFileProfiles struct { + DefaultProfile string `yaml:"default-profile"` + Profiles map[string]ConfigFileProfile `yaml:"profiles"` +} + +func GetConfigDirPath() (string, error) { + configDir, err := os.UserConfigDir() + if err != nil { + return "", err + } + return path.Join(configDir, "appclacks"), nil + +} + +func GetConfigFilePath() (string, error) { + configDir, err := GetConfigDirPath() + if err != nil { + return "", err + } + return path.Join(configDir, "appclacks.yaml"), err +} + +func ReadConfig(path string) (ConfigFileProfiles, error) { + configFile, err := os.ReadFile(path) + if err != nil { + return ConfigFileProfiles{}, err + } + var profiles ConfigFileProfiles + err = yaml.Unmarshal(configFile, &profiles) + if err != nil { + return ConfigFileProfiles{}, fmt.Errorf("Invalid YAML file at %s: %w", path, err) + } + if profiles.Profiles == nil { + profiles.Profiles = make(map[string]ConfigFileProfile) + } + return profiles, nil +} + +func loadConnInfosFromEnv(connInfos *ConnexionInfos) { + + if os.Getenv("APPCLACKS_API_ENDPOINT") != "" { + connInfos.setBaseURL(os.Getenv("APPCLACKS_API_ENDPOINT")) + } + + if os.Getenv("APPCLACKS_ORGANIZATION_ID") != "" && os.Getenv("APPCLACKS_TOKEN") != "" { + connInfos.setTokenAuth(os.Getenv("APPCLACKS_ORGANIZATION_ID"), os.Getenv("APPCLACKS_TOKEN")) + return + } + + if os.Getenv("APPCLACKS_ACCOUNT_EMAIL") != "" && os.Getenv("APPCLACKS_ACCOUNT_PASSWORD") != ""{ + connInfos.setPasswordAuth(os.Getenv("APPCLACKS_ACCOUNT_EMAIL"), os.Getenv("APPCLACKS_ACCOUNT_PASSWORD")) + return + } + +} + +func loadConnInfosFromConfigFile(connInfos *ConnexionInfos) (error) { + configPath, err := GetConfigFilePath() + if err != nil { + return err + } + config, err := ReadConfig(configPath) + if err != nil { + return err + } + if len(config.Profiles) == 0 { + return fmt.Errorf("The configuration file %s is empty", configPath) + } + if connInfos.profile == "" { + connInfos.profile = config.DefaultProfile + } + if connInfos.profile == "" { + return fmt.Errorf("No profile selected and no default profile specified in the configuration file %s", configPath) + } + configProfile, ok := config.Profiles[connInfos.profile] + if !ok { + return fmt.Errorf("Profile %s not found in the configuration file %s", connInfos.profile, configPath) + } + + if configProfile.APIToken == "" || configProfile.OrganizationID == "" { + return fmt.Errorf("Profile %s in the configuration file %s is not complete. APIToken or OrganizationID is missing", connInfos.profile, configPath) + } + + connInfos.setTokenAuth(configProfile.OrganizationID, configProfile.APIToken) + + return nil +} + diff --git a/cmd/healthcheck.go b/cmd/healthcheck.go index bde9899..cc62e7c 100644 --- a/cmd/healthcheck.go +++ b/cmd/healthcheck.go @@ -47,7 +47,7 @@ func getHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - healthcheck, err := client.GetHealthcheck(ctx, input) + healthcheck, err := client.GetHealthcheckWithContext(ctx, input) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(&healthcheck) @@ -77,7 +77,7 @@ func deleteHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.DeleteHealthcheck(ctx, input) + result, err := client.DeleteHealthcheckWithContext(ctx, input) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) @@ -109,7 +109,7 @@ func listHealthchecksCmd() *cobra.Command { client := buildClient() ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.ListHealthchecks(ctx) + result, err := client.ListHealthchecksWithContext(ctx) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/healthcheck_command.go b/cmd/healthcheck_command.go index ab7b1a0..ebf6dca 100644 --- a/cmd/healthcheck_command.go +++ b/cmd/healthcheck_command.go @@ -41,7 +41,7 @@ func createCommandHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - healthcheck, err := client.CreateCommandHealthcheck(ctx, payload) + healthcheck, err := client.CreateCommandHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(healthcheck) @@ -110,7 +110,7 @@ func updateCommandHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.UpdateCommandHealthcheck(ctx, payload) + result, err := client.UpdateCommandHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/healthcheck_dns.go b/cmd/healthcheck_dns.go index 13f129c..dddf279 100644 --- a/cmd/healthcheck_dns.go +++ b/cmd/healthcheck_dns.go @@ -42,7 +42,7 @@ func createDNSHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - healthcheck, err := client.CreateDNSHealthcheck(ctx, payload) + healthcheck, err := client.CreateDNSHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(healthcheck) @@ -111,7 +111,7 @@ func updateDNSHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.UpdateDNSHealthcheck(ctx, payload) + result, err := client.UpdateDNSHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/healthcheck_http.go b/cmd/healthcheck_http.go index 623be7a..8f93b48 100644 --- a/cmd/healthcheck_http.go +++ b/cmd/healthcheck_http.go @@ -73,7 +73,7 @@ func createHTTPHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - healthcheck, err := client.CreateHTTPHealthcheck(ctx, payload) + healthcheck, err := client.CreateHTTPHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(healthcheck) @@ -196,7 +196,7 @@ func updateHTTPHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.UpdateHTTPHealthcheck(ctx, payload) + result, err := client.UpdateHTTPHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/healthcheck_tcp.go b/cmd/healthcheck_tcp.go index 2308214..ec3000f 100644 --- a/cmd/healthcheck_tcp.go +++ b/cmd/healthcheck_tcp.go @@ -47,7 +47,7 @@ func createTCPHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - healthcheck, err := client.CreateTCPHealthcheck(ctx, payload) + healthcheck, err := client.CreateTCPHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(healthcheck) @@ -123,7 +123,7 @@ func updateTCPHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.UpdateTCPHealthcheck(ctx, payload) + result, err := client.UpdateTCPHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/healthcheck_tls.go b/cmd/healthcheck_tls.go index 97994c3..544caf3 100644 --- a/cmd/healthcheck_tls.go +++ b/cmd/healthcheck_tls.go @@ -56,7 +56,7 @@ func createTLSHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - healthcheck, err := client.CreateTLSHealthcheck(ctx, payload) + healthcheck, err := client.CreateTLSHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(healthcheck) @@ -146,7 +146,7 @@ func updateTLSHealthcheckCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.UpdateTLSHealthcheck(ctx, payload) + result, err := client.UpdateTLSHealthcheckWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/login.go b/cmd/login.go index b82b586..4369c72 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -11,7 +11,6 @@ import ( "golang.org/x/term" "gopkg.in/yaml.v3" - "github.com/appclacks/cli/client" apitypes "github.com/appclacks/go-types" "github.com/spf13/cobra" ) @@ -22,18 +21,18 @@ func loginCmd() *cobra.Command { Short: "Log in to the Appclacks Cloud platform", Run: func(cmd *cobra.Command, args []string) { reader := bufio.NewReader(os.Stdin) - appclacksConfigDir, err := client.GetConfigDirPath() + appclacksConfigDir, err := GetConfigDirPath() exitIfError(err) err = os.MkdirAll(appclacksConfigDir, os.ModePerm) exitIfError(err) - filepath, err := client.GetConfigFilePath() + filepath, err := GetConfigFilePath() exitIfError(err) fp, err := os.OpenFile(filepath, os.O_RDONLY|os.O_CREATE, 0600) exitIfError(err) fp.Close() - profiles, err := client.ReadConfig(filepath) + profiles, err := ReadConfig(filepath) exitIfError(err) fmt.Printf("The new profile will be written into the file located at: %s\n\n", filepath) @@ -51,12 +50,12 @@ func loginCmd() *cobra.Command { fmt.Println("") password := strings.TrimSpace(string(bytePassword)) - cliClient, err := client.New(appclacksURL, client.WithUserPassword(client.AccountEmail(email), client.AccountPassword(password))) - exitIfError(err) + cliClient := buildClientByPassword(email, password) ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) - org, err := cliClient.GetOrganizationForAccount(ctx) + org, err := cliClient.GetOrganizationForAccountWithContext(ctx) + exitIfError(err) cancel() @@ -71,16 +70,16 @@ func loginCmd() *cobra.Command { Actions: []string{"*"}, }, } - apiToken, err := cliClient.CreateAPIToken(ctx, payload) + apiToken, err := cliClient.CreateAPITokenWithContext(ctx, payload) exitIfError(err) cancel() - profile := client.ConfigFileProfile{ + profile := ConfigFileProfile{ OrganizationID: org.ID, APIToken: apiToken.Token, } if profiles.Profiles == nil { - profiles.Profiles = make(map[string]client.ConfigFileProfile) + profiles.Profiles = make(map[string]ConfigFileProfile) } profiles.Profiles[profileName] = profile diff --git a/cmd/metrics.go b/cmd/metrics.go index f8820b1..f5fe022 100644 --- a/cmd/metrics.go +++ b/cmd/metrics.go @@ -16,7 +16,7 @@ func getHealthchecksMetricsCmd() *cobra.Command { client := buildClient() ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - metrics, err := client.GetHealthchecksMetrics(ctx) + metrics, err := client.GetHealthchecksMetricsWithContext(ctx) exitIfError(err) fmt.Print(metrics) os.Exit(0) diff --git a/cmd/result.go b/cmd/result.go index 761cd64..9e7e476 100644 --- a/cmd/result.go +++ b/cmd/result.go @@ -54,7 +54,7 @@ func listHealthckecksResultsCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.ListHealthchecksResults(ctx, input) + result, err := client.ListHealthchecksResultsWithContext(ctx, input) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) diff --git a/cmd/root.go b/cmd/root.go index f8013ea..2215f71 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -127,7 +127,55 @@ func Execute() error { } func buildClient() *client.Client { - client, err := client.New(appclacksURL, client.WithProfile(profile)) - exitIfError(err) + + var client *client.Client + + connInfos := CreateConnInfos() + if profile != "" { + connInfos.profile = profile + err := loadConnInfosFromConfigFile(connInfos) + exitIfError(err) + } + + if connInfos.TypeAuth == "noAuth" { + loadConnInfosFromEnv(connInfos) + } + + if connInfos.TypeAuth == "noAuth" { + err := loadConnInfosFromConfigFile(connInfos) + exitIfError(err) + } + + if connInfos.TypeAuth == "password"{ + client = buildClientByPassword(connInfos.identifier, connInfos.secret) + } + + if connInfos.TypeAuth == "token"{ + client = buildClientByToken(connInfos.identifier, connInfos.secret) + } + + if client == nil { + client = buildClientNoAuth() + } + return client } + +func buildClientByPassword(email string, password string) *client.Client{ + clientOptions := client.GetDefaultClientOptions().UsePasswordAuth(email, password) + clientOptions.SetBaseUrl(appclacksURL) + return client.New(clientOptions) + +} + +func buildClientByToken(orgID string, token string) *client.Client{ + clientOptions := client.GetDefaultClientOptions().UseTokenAuth(orgID, token) + clientOptions.SetBaseUrl(appclacksURL) + return client.New(clientOptions) +} + +func buildClientNoAuth() *client.Client { + clientOptions := client.GetDefaultClientOptions() + clientOptions.SetBaseUrl(appclacksURL) + return client.New(clientOptions) +} \ No newline at end of file diff --git a/cmd/token.go b/cmd/token.go index d879b8e..127aca2 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -32,7 +32,7 @@ func createAPITokenCmd() *cobra.Command { Actions: permissions, }, } - result, err := client.CreateAPIToken(ctx, payload) + result, err := client.CreateAPITokenWithContext(ctx, payload) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) @@ -65,7 +65,7 @@ func listAPITokensCmd() *cobra.Command { client := buildClient() ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.ListAPITokens(ctx) + result, err := client.ListAPITokensWithContext(ctx) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result) @@ -99,7 +99,7 @@ func getAPITokenCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - token, err := client.GetAPIToken(ctx, input) + token, err := client.GetAPITokenWithContext(ctx, input) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(token) @@ -133,7 +133,7 @@ func deleteAPITokenCmd() *cobra.Command { } ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) defer cancel() - result, err := client.DeleteAPIToken(ctx, input) + result, err := client.DeleteAPITokenWithContext(ctx, input) exitIfError(err) if outputFormat == "json" { json, err := json.Marshal(result)