diff --git a/api/auth/auth.go b/api/auth/auth.go index 5f5031c..4d5fcdf 100644 --- a/api/auth/auth.go +++ b/api/auth/auth.go @@ -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" @@ -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 } @@ -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 diff --git a/api/auth/types.go b/api/auth/types.go index 0fc45e0..6dc776c 100644 --- a/api/auth/types.go +++ b/api/auth/types.go @@ -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"` diff --git a/api/cert/cert.go b/api/cert/cert.go index f5c1591..82dd7af 100644 --- a/api/cert/cert.go +++ b/api/cert/cert.go @@ -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" @@ -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 } @@ -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 } @@ -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 } diff --git a/api/cert/types.go b/api/cert/types.go index bd300f3..6b770f6 100644 --- a/api/cert/types.go +++ b/api/cert/types.go @@ -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"` @@ -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"` @@ -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"` diff --git a/api/event/event.go b/api/event/event.go index 21a7713..80d86b7 100644 --- a/api/event/event.go +++ b/api/event/event.go @@ -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" @@ -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 } diff --git a/api/event/types.go b/api/event/types.go index c494b99..b351828 100644 --- a/api/event/types.go +++ b/api/event/types.go @@ -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"` diff --git a/api/iam/iam.go b/api/iam/iam.go index 9f947d3..f76f53e 100644 --- a/api/iam/iam.go +++ b/api/iam/iam.go @@ -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" @@ -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 } @@ -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 } @@ -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 @@ -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 diff --git a/api/iam/types.go b/api/iam/types.go index 16bca1f..4be8a11 100644 --- a/api/iam/types.go +++ b/api/iam/types.go @@ -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"` @@ -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"` diff --git a/api/log/log.go b/api/log/log.go index d1543b9..62c4d1e 100644 --- a/api/log/log.go +++ b/api/log/log.go @@ -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" @@ -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 } diff --git a/api/log/types.go b/api/log/types.go index 0985671..235f57a 100644 --- a/api/log/types.go +++ b/api/log/types.go @@ -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"` -} diff --git a/api/note/note.go b/api/note/note.go index a45363c..89dcba7 100644 --- a/api/note/note.go +++ b/api/note/note.go @@ -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" @@ -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 } diff --git a/api/note/types.go b/api/note/types.go index ef39c92..803a09c 100644 --- a/api/note/types.go +++ b/api/note/types.go @@ -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"` diff --git a/api/packages/packages.go b/api/packages/packages.go index 8b06d1f..c498edb 100644 --- a/api/packages/packages.go +++ b/api/packages/packages.go @@ -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" @@ -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 } @@ -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 } @@ -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 { diff --git a/api/packages/types.go b/api/packages/types.go index bcb42b5..56fe7f0 100644 --- a/api/packages/types.go +++ b/api/packages/types.go @@ -17,24 +17,6 @@ type PythonPackage struct { Owner string `json:"owner"` } -type SystemPackageListResponse struct { - Count int `json:"count"` - Current int `json:"current"` - Next int `json:"next"` - Previous string `json:"previous"` - Last int `json:"last"` - Results []SystemPackageDetail `json:"results"` -} - -type PythonPackageListResponse struct { - Count int `json:"count"` - Current int `json:"current"` - Next int `json:"next"` - Previous string `json:"previous"` - Last int `json:"last"` - Results []PythonPackageDetail `json:"results"` -} - type PythonPackageDetail struct { ID string `json:"id"` Name string `json:"name"` diff --git a/api/server/server.go b/api/server/server.go index 42d6236..3c7eef8 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -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" @@ -28,7 +29,7 @@ func GetServerList(ac *client.AlpaconClient) ([]ServerAttributes, error) { return nil, err } - var response ServerListResponse + var response api.ListResponse[ServerDetails] if err = json.Unmarshal(responseBody, &response); err != nil { return nil, err } @@ -89,7 +90,7 @@ func GetServerIDByName(ac *client.AlpaconClient, serverName string) (string, err return "", err } - var response ServerListResponse + var response api.ListResponse[ServerDetails] err = json.Unmarshal(body, &response) if err != nil { return "", err diff --git a/api/server/types.go b/api/server/types.go index 23000ec..be9eb88 100644 --- a/api/server/types.go +++ b/api/server/types.go @@ -25,15 +25,6 @@ type ServerCreatedResponse struct { Groups []string `json:"groups"` } -type ServerListResponse struct { - Count int `json:"count"` - Current int `json:"current"` - Next int `json:"next"` - Previous string `json:"previous"` - Last int `json:"last"` - Results []ServerDetails `json:"results"` -} - type ServerStatus struct { Code string `json:"code"` Icon string `json:"icon"` diff --git a/api/types.go b/api/types.go new file mode 100644 index 0000000..a5b4fbd --- /dev/null +++ b/api/types.go @@ -0,0 +1,10 @@ +package api + +type ListResponse[T any] struct { + Count int `json:"count"` + Current int `json:"current"` + Next int `json:"next"` + Previous string `json:"previous"` + Last int `json:"last"` + Results []T `json:"results"` +}