diff --git a/pkg/auth.go b/pkg/auth.go index 2c55559..93c5449 100644 --- a/pkg/auth.go +++ b/pkg/auth.go @@ -9,7 +9,6 @@ import ( // GetMe Return information about the logged in User func (sdk mfSDK) GetMe() (UserRes, error) { - var ur UserRes endpoint := "auth/me" url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) @@ -30,7 +29,6 @@ func (sdk mfSDK) GetMe() (UserRes, error) { // GenerateUserAPIKey generates (or re-generate) an API Key for logged in User. // To generate this without logging into Memfault, you may use HTTP Basic Auth to call this API. func (sdk mfSDK) GenerateUserAPIKey() (UserAPIKeyRes, error) { - var akr UserAPIKeyRes endpoint := "auth/api_key" url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) payload := strings.NewReader(``) @@ -52,7 +50,6 @@ func (sdk mfSDK) GenerateUserAPIKey() (UserAPIKeyRes, error) { // GetUserAPIKey Get a previously generated API Key for the logged in User func (sdk mfSDK) GetUserAPIKey() (UserAPIKeyRes, error) { - var akr UserAPIKeyRes endpoint := "auth/api_key" url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) diff --git a/pkg/cohorts.go b/pkg/cohorts.go index d860178..892b09c 100644 --- a/pkg/cohorts.go +++ b/pkg/cohorts.go @@ -8,18 +8,8 @@ import ( "strings" ) -var ( - ccr CreateCohortRes - lcr ListCohortRes - er ErrorRes -) - // CreateCohort creates a Cohort for the given Project func (sdk mfSDK) CreateCohort(project Project, cohort Cohort) (CreateCohortRes, error) { - var ( - ccr CreateCohortRes - er ErrorRes - ) slug, err := sdk.getProjectSlugByName(project.Name) if err != nil { return ccr, err @@ -33,7 +23,6 @@ func (sdk mfSDK) CreateCohort(project Project, cohort Cohort) (CreateCohortRes, return ccr, err } payload := strings.NewReader(string(data)) - endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + string(slug) + "/cohorts" url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) @@ -51,7 +40,6 @@ func (sdk mfSDK) CreateCohort(project Project, cohort Cohort) (CreateCohortRes, } return ccr, errors.New(string(er.Error.Message)) } - if err := json.Unmarshal(resp, &ccr); err != nil { return ccr, err } @@ -68,7 +56,6 @@ func (sdk mfSDK) ListCohorts(project Project) (ListCohortRes, error) { if err != nil { return lcr, err } - endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + string(slug) + "/cohorts" url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) @@ -86,9 +73,111 @@ func (sdk mfSDK) ListCohorts(project Project) (ListCohortRes, error) { } return lcr, errors.New(string(er.Error.Message)) } - if err := json.Unmarshal(resp, &lcr); err != nil { return lcr, err } return lcr, nil } + +// Retrieve a single Cohort +func (sdk mfSDK) RetrieveCohorts(project Project, cohort Cohort) (CreateCohortRes, error) { + projectslug, err := sdk.getProjectSlugByName(project.Name) + if err != nil { + return ccr, err + } + organizationslug, err := sdk.getOrganizationSlug(false) + if err != nil { + return ccr, err + } + cohortslug, err := sdk.getCohortSlug(project, cohort.Name) + if err != nil { + return ccr, err + } + endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + string(projectslug) + "/cohorts/" + string(cohortslug) + url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return ccr, err + } + resp, statusCode, err := sdk.makeRequest(req) + if err != nil { + return ccr, err + } + if statusCode != 200 { + if err := json.Unmarshal(resp, &er); err != nil { + return ccr, err + } + return ccr, errors.New(string(er.Error.Message)) + } + if err := json.Unmarshal(resp, &ccr); err != nil { + return ccr, err + } + return ccr, nil +} + +// UpdateCohorts Update a single Cohort +func (sdk mfSDK) UpdateCohorts(project Project, cohort Cohort, cohortslug string) (CreateCohortRes, error) { + projectslug, err := sdk.getProjectSlugByName(project.Name) + if err != nil { + return ccr, err + } + organizationslug, err := sdk.getOrganizationSlug(false) + if err != nil { + return ccr, err + } + data, err := json.Marshal(cohort) + if err != nil { + return ccr, err + } + payload := strings.NewReader(string(data)) + endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + string(projectslug) + "/cohorts/" + string(cohortslug) + url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) + + req, err := http.NewRequest(http.MethodPatch, url, payload) + if err != nil { + return ccr, err + } + resp, statusCode, err := sdk.makeRequest(req) + if err != nil { + return ccr, err + } + if statusCode != 200 { + if err := json.Unmarshal(resp, &er); err != nil { + return ccr, err + } + return ccr, errors.New(string(er.Error.Message)) + } + if err := json.Unmarshal(resp, &ccr); err != nil { + return ccr, err + } + return ccr, nil +} + +//DeleteCohorts Delete a single Cohort +func (sdk mfSDK) DeleteCohorts(project Project, cohort Cohort) (string, error) { + projectslug, err := sdk.getProjectSlugByName(project.Name) + if err != nil { + return "", err + } + organizationslug, err := sdk.getOrganizationSlug(false) + if err != nil { + return "", err + } + cohortslug, err := sdk.getCohortSlug(project, cohort.Name) + if err != nil { + return "", err + } + endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + string(projectslug) + "/cohorts/" + string(cohortslug) + url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) + + req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + return "", err + } + _, _, err = sdk.makeRequest(req) + if err != nil { + return "", err + } + return "DELETED", nil +} diff --git a/pkg/memfault.go b/pkg/memfault.go index a5ab9b7..94e35ea 100644 --- a/pkg/memfault.go +++ b/pkg/memfault.go @@ -18,6 +18,13 @@ type ContentType string var _ SDK = (*mfSDK)(nil) +var ( + cpr CreateProjectRes + lpr ListProjectRes + uakr UserAPIKeyRes + er ErrorRes +) + // SDK contains Mainflux API. type SDK interface { // GetMe Return information about the logged in User @@ -56,6 +63,12 @@ type SDK interface { CreateCohort(project Project, cohort Cohort) (CreateCohortRes, error) ListCohorts(project Project) (ListCohortRes, error) + + RetrieveCohorts(project Project, cohort Cohort) (CreateCohortRes, error) + + UpdateCohorts(project Project, cohort Cohort, cohortslug string) (CreateCohortRes, error) + + DeleteCohorts(project Project, cohort Cohort) (string, error) } // Credentials contains the credentials @@ -97,6 +110,7 @@ type Project struct { Platform string `json:"platform"` } +// Cohort struct type Cohort struct { Name string `json:"name,omitempty"` Slug string `json:"slug,omitempty"` @@ -129,7 +143,6 @@ func (sdk mfSDK) makeRequest(req *http.Request) ([]byte, int, error) { if err != nil { return nil, 0, err } - body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, 0, err @@ -142,9 +155,7 @@ func (sdk mfSDK) sendRequest(req *http.Request, token string) (*http.Response, e if token != "" { req.Header.Add("Authorization", "Basic "+token) } - req.Header.Add("Content-Type", "application/json") - res, err := sdk.client.Do(req) if err != nil { return res, err @@ -169,7 +180,6 @@ func (sdk mfSDK) authenticate() (string, error) { func (sdk mfSDK) getOrganizationSlug(multiple bool) (string, error) { endpoint := "auth/me" url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint) - req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return "", err @@ -220,3 +230,15 @@ func (sdk mfSDK) getProjectSlugByID(id int) (string, error) { } return "", nil } +func (sdk mfSDK) getCohortSlug(project Project, name string) (string, error) { + cohorts, err := sdk.ListCohorts(project) + if err != nil { + return "", nil + } + for _, cohort := range cohorts.Data { + if name == cohort.Name { + return cohort.Slug, nil + } + } + return "", nil +} diff --git a/pkg/projects.go b/pkg/projects.go index 0c3c220..50f1e22 100644 --- a/pkg/projects.go +++ b/pkg/projects.go @@ -11,9 +11,6 @@ import ( // CreateProject creates a Project under the given Organization func (sdk mfSDK) CreateProject(project Project) (CreateProjectRes, error) { var exists bool - var cpr CreateProjectRes - var er ErrorRes - slug, err := sdk.getProjectSlugByName(project.Name) if err != nil { return cpr, nil @@ -56,7 +53,6 @@ func (sdk mfSDK) CreateProject(project Project) (CreateProjectRes, error) { // ListProject list the Projects under a given Organization func (sdk mfSDK) ListProject() (ListProjectRes, error) { - var lpr ListProjectRes organizationslug, err := sdk.getOrganizationSlug(false) if err != nil { return lpr, err @@ -80,7 +76,6 @@ func (sdk mfSDK) ListProject() (ListProjectRes, error) { // RetrieveProject retrieve a Project under a given Organization func (sdk mfSDK) RetrieveProject(projectSlug string) (CreateProjectRes, error) { - var cpr CreateProjectRes organizationslug, err := sdk.getOrganizationSlug(false) if err != nil { return cpr, err @@ -104,7 +99,6 @@ func (sdk mfSDK) RetrieveProject(projectSlug string) (CreateProjectRes, error) { // UpdateProject updates a Project under a given Organization func (sdk mfSDK) UpdateProject(project Project) (CreateProjectRes, error) { - var cpr CreateProjectRes organizationslug, err := sdk.getOrganizationSlug(false) if err != nil { return cpr, err @@ -165,7 +159,6 @@ func (sdk mfSDK) DeleteProject(project Project) (string, error) { // GetProjectClientKey Return the Project Client Key func (sdk mfSDK) GetProjectClientKey(projectSlug string) (UserAPIKeyRes, error) { - var uakr UserAPIKeyRes organizationslug, err := sdk.getOrganizationSlug(false) if err != nil { return uakr, err @@ -189,7 +182,6 @@ func (sdk mfSDK) GetProjectClientKey(projectSlug string) (UserAPIKeyRes, error) // RefreshProjectClientKey Regenerate the Project Client Key func (sdk mfSDK) RefreshProjectClientKey(projectSlug string) (UserAPIKeyRes, error) { - var uakr UserAPIKeyRes organizationslug, err := sdk.getOrganizationSlug(false) if err != nil { return uakr, err diff --git a/pkg/responses.go b/pkg/responses.go index 2cc5fb7..0c08d11 100644 --- a/pkg/responses.go +++ b/pkg/responses.go @@ -128,6 +128,7 @@ type ListProjectRes struct { Paging paging `json:"paging,omitempty"` } +// CohortData struct type CohortData struct { ByPassPrimarySoftwareTypeCheck bool `json:"bypass_primary_software_type_check,omitempty"` ByPassVersionChecks bool `json:"bypass_version_checks,omitempty"`