Skip to content

Commit

Permalink
Refactor types to leverage generics in Go
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Mar 4, 2024
1 parent 4e50e44 commit 9ddec43
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 143 deletions.
5 changes: 3 additions & 2 deletions api/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alpacanetworks/alpacon-cli/api"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/config"
"github.com/alpacanetworks/alpacon-cli/utils"
Expand Down Expand Up @@ -116,7 +117,7 @@ func GetAPITokenList(ac *client.AlpaconClient) ([]APITokenAttributes, error) {
return nil, err
}

var response APITokenListResponse
var response api.ListResponse[APITokenResponse]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,7 +149,7 @@ func getAPITokenIDByName(ac *client.AlpaconClient, tokenName string) (string, er
return "", err
}

var response APITokenListResponse
var response api.ListResponse[APITokenResponse]
err = json.Unmarshal(body, &response)
if err != nil {
return "", err
Expand Down
9 changes: 0 additions & 9 deletions api/auth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ type APITokenRequest struct {
ExpiresAt *string `json:"expires_at"`
}

type APITokenListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []APITokenResponse `json:"results"`
}

type APITokenResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down
7 changes: 4 additions & 3 deletions api/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/alpacanetworks/alpacon-cli/api"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
"path"
Expand Down Expand Up @@ -77,7 +78,7 @@ func GetCSRList(ac *client.AlpaconClient, state string) ([]CSRAttributes, error)
return nil, err
}

var response CSRListResponse
var response api.ListResponse[CSRResponse]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,7 +122,7 @@ func GetAuthorityList(ac *client.AlpaconClient) ([]AuthorityAttributes, error) {
return nil, err
}

var response AuthorityListResponse
var response api.ListResponse[AuthorityResponse]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down Expand Up @@ -221,7 +222,7 @@ func GetCertificateList(ac *client.AlpaconClient) ([]CertificateAttributes, erro
return nil, err
}

var response CertificateListResponse
var response api.ListResponse[Certificate]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down
27 changes: 0 additions & 27 deletions api/cert/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ type AuthorityCreateResponse struct {
UpdatedAt time.Time `json:"updated_at"`
}

type AuthorityListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []AuthorityResponse `json:"results"`
}

type AuthorityResponse struct {
Id string `json:"id"`
Name string `json:"name"`
Expand Down Expand Up @@ -92,15 +83,6 @@ type CSRSubmit struct {
CsrText string `json:"csr_text"`
}

type CSRListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []CSRResponse `json:"results"`
}

type CSRResponse struct {
Id string `json:"id"`
Authority string `json:"authority"`
Expand Down Expand Up @@ -128,15 +110,6 @@ type CSRAttributes struct {
RequestedDate string `json:"requested_date"`
}

type CertificateListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []Certificate `json:"results"`
}

type Certificate struct {
Id string `json:"id"`
Authority string `json:"authority"`
Expand Down
3 changes: 2 additions & 1 deletion api/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alpacanetworks/alpacon-cli/api"
"github.com/alpacanetworks/alpacon-cli/api/iam"
"github.com/alpacanetworks/alpacon-cli/api/server"
"github.com/alpacanetworks/alpacon-cli/client"
Expand Down Expand Up @@ -41,7 +42,7 @@ func GetEventList(ac *client.AlpaconClient, pageSize int, serverName string, use
return nil, err
}

var response EventListResponse
var response api.ListResponse[EventDetails]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down
9 changes: 0 additions & 9 deletions api/event/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ type EventAttributes struct {
RequestedAt string `json:"requested_at"`
}

type EventListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous int `json:"previous"`
Last int `json:"last"`
Results []EventDetails `json:"results"`
}

type EventDetails struct {
ID string `json:"id"`
Shell string `json:"shell"`
Expand Down
9 changes: 5 additions & 4 deletions api/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alpacanetworks/alpacon-cli/api"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
"strconv"
Expand All @@ -30,7 +31,7 @@ func GetUserList(ac *client.AlpaconClient) ([]UserAttributes, error) {
return nil, err
}

var response UserListResponse
var response api.ListResponse[UserResponse]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down Expand Up @@ -71,7 +72,7 @@ func GetGroupList(ac *client.AlpaconClient) ([]GroupAttributes, error) {
return nil, err
}

var response GroupListResponse
var response api.ListResponse[GroupResponse]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down Expand Up @@ -298,7 +299,7 @@ func GetUserIDByName(ac *client.AlpaconClient, userName string) (string, error)
return "", err
}

var response UserListResponse
var response api.ListResponse[UserResponse]
err = json.Unmarshal(responseBody, &response)
if err != nil {
return "", err
Expand Down Expand Up @@ -335,7 +336,7 @@ func GetGroupIDByName(ac *client.AlpaconClient, groupName string) (string, error
return "", err
}

var response GroupListResponse
var response api.ListResponse[GroupResponse]
err = json.Unmarshal(responseBody, &response)
if err != nil {
return "", err
Expand Down
18 changes: 0 additions & 18 deletions api/iam/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ type UserDetailAttributes struct {
LDAPStatus string `json:"ldap_status"`
}

type UserListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []UserResponse `json:"results"`
}

type UserResponse struct {
ID string `json:"id"`
Username string `json:"username"`
Expand Down Expand Up @@ -118,15 +109,6 @@ type GroupDetailAttributes struct {
ServersNames []string `json:"servers_names"`
}

type GroupListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []GroupResponse `json:"results"`
}

type GroupResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down
3 changes: 2 additions & 1 deletion api/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"encoding/json"
"fmt"
"github.com/alpacanetworks/alpacon-cli/api"
"github.com/alpacanetworks/alpacon-cli/api/server"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
Expand All @@ -27,7 +28,7 @@ func GetSystemLogList(ac *client.AlpaconClient, serverName string, pageSize int)
return nil, err
}

var response LogListResponse
var response api.ListResponse[LogEntry]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down
9 changes: 0 additions & 9 deletions api/log/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,3 @@ type LogEntry struct {
Server string `json:"server"`
ServerName string `json:"server_name"`
}

type LogListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []LogEntry `json:"results"`
}
3 changes: 2 additions & 1 deletion api/note/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package note
import (
"encoding/json"
"fmt"
"github.com/alpacanetworks/alpacon-cli/api"
"github.com/alpacanetworks/alpacon-cli/api/iam"
"github.com/alpacanetworks/alpacon-cli/api/server"
"github.com/alpacanetworks/alpacon-cli/client"
Expand Down Expand Up @@ -34,7 +35,7 @@ func GetNoteList(ac *client.AlpaconClient, serverName string, pageSize int) ([]N
return nil, err
}

var response NoteListResponse
var response api.ListResponse[NoteDetails]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down
9 changes: 0 additions & 9 deletions api/note/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package note

type NoteListResponse struct {
Count int `json:"count"`
Current int `json:"current"`
Next int `json:"next"`
Previous string `json:"previous"`
Last int `json:"last"`
Results []NoteDetails `json:"results"`
}

type NoteDetails struct {
ID string `json:"id"`
Server string `json:"server"`
Expand Down
54 changes: 33 additions & 21 deletions api/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/alpacanetworks/alpacon-cli/api"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
"io"
Expand Down Expand Up @@ -33,7 +34,7 @@ func GetSystemPackageEntry(ac *client.AlpaconClient) ([]SystemPackage, error) {
return nil, err
}

var response SystemPackageListResponse
var response api.ListResponse[SystemPackageDetail]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand Down Expand Up @@ -71,7 +72,7 @@ func GetPythonPackageEntry(ac *client.AlpaconClient) ([]PythonPackage, error) {
return nil, err
}

var response PythonPackageListResponse
var response api.ListResponse[PythonPackageDetail]
if err = json.Unmarshal(responseBody, &response); err != nil {
return nil, err
}
Expand All @@ -97,32 +98,43 @@ func GetPythonPackageEntry(ac *client.AlpaconClient) ([]PythonPackage, error) {

func GetPackageIDByName(ac *client.AlpaconClient, fileName string, packageType string) (string, error) {
var url string
params := map[string]string{
"name": fileName,
}

if packageType == "python" {
url = pythonPackageEntryURL
var response api.ListResponse[PythonPackageDetail]
body, err := ac.SendGetRequest(utils.BuildURL(url, "", params))
if err != nil {
return "", err
}
err = json.Unmarshal(body, &response)
if err != nil {
return "", err
}

if response.Count == 0 {
return "", errors.New("no server found with the given name")
}
return response.Results[0].ID, nil
} else {
url = systemPackageEntryURL
}

params := map[string]string{
"name": fileName,
}
body, err := ac.SendGetRequest(utils.BuildURL(url, "", params))
if err != nil {
return "", err
}

var response PythonPackageListResponse
err = json.Unmarshal(body, &response)
if err != nil {
return "", err
}
var response api.ListResponse[SystemPackageDetail]
body, err := ac.SendGetRequest(utils.BuildURL(url, "", params))
if err != nil {
return "", err
}
err = json.Unmarshal(body, &response)
if err != nil {
return "", err
}

if response.Count == 0 {
return "", errors.New("no server found with the given name")
if response.Count == 0 {
return "", errors.New("no server found with the given name")
}
return response.Results[0].ID, nil
}

return response.Results[0].ID, nil
}

func UploadPackage(ac *client.AlpaconClient, file string, packageType string) error {
Expand Down
Loading

0 comments on commit 9ddec43

Please sign in to comment.