From 5a999e3ef3ea832f141180dd33e205cdb4236630 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 21 Aug 2023 11:07:02 +0900 Subject: [PATCH] fix ssh keys after api changes --- service.go | 150 ++++++++++++++++++++++++------------------------ service_test.go | 20 +++---- 2 files changed, 85 insertions(+), 85 deletions(-) diff --git a/service.go b/service.go index d5d88d3..3482fed 100644 --- a/service.go +++ b/service.go @@ -52,10 +52,10 @@ type ( Command string } - ServiceSSHKey struct { - ID int64 `json:"lineID"` - KeyName string `json:"name"` - PublicKey string `json:"keys"` + ServiceSSHPublicKey struct { + ID int64 `json:"indexID"` + Name string `json:"name"` + Key string `json:"key"` } ServiceFirewallPort struct { @@ -67,62 +67,62 @@ type ( ServiceFirewallPortProtocol string Service struct { - ID string `json:"vmID"` - ProjectID string `json:"projectID"` - ServerName string `json:"displayName"` - ServerType string `json:"serverType"` - TemplateID int64 `json:"template"` - Version string `json:"selected_software_tag"` - ProviderName string `json:"provider"` - Datacenter string `json:"datacenter"` - SupportLevel string `json:"support"` - AdminEmail string `json:"email"` - Category string `json:"category"` - Status string `json:"status"` - DeploymentStatus string `json:"deploymentStatus"` - DeploymentStartedAt string `json:"deploymentStart"` - DeploymentEndedAt string `json:"deploymentEnd"` - CreatorName string `json:"creatorName"` - CreatedAt string `json:"creationDate"` - IPV4 string `json:"ipv4"` - IPV6 string `json:"ipv6"` - CNAME string `json:"cname"` - CustomDomainNames []string `json:"customDomainNames"` - SSHKeys []ServiceSSHKey `json:"sshKeys"` - Country string `json:"country"` - City string `json:"city"` - AdminUser string `json:"adminUser"` - RootAppPath string `json:"rootAppPath"` - GlobalIP string `json:"globalIP"` - AdminExternalPort int64 `json:"adminExternalPort"` - AdminInternalPort int64 `json:"adminInternalPort"` - TrafficOutgoing int64 `json:"traffic_outgoing"` - TrafficIncoming int64 `json:"traffic_ingoing"` - TrafficIncluded int64 `json:"traffic_included"` - Cores int64 `json:"cores"` - RAMSizeGB string `json:"ramGB"` - StorageSizeGB int64 `json:"storageSizeGB"` - PricePerHour string `json:"pricePerHour"` - AppAutoUpdatesEnabled NumberAsBool `json:"app_AutoUpdate_Enabled"` - AppAutoUpdatesDayOfWeek int64 `json:"app_AutoUpdate_DayOfWeek"` - AppAutoUpdatesHour int64 `json:"app_AutoUpdate_Hour"` - AppAutoUpdatesMinute int64 `json:"app_AutoUpdate_Minute"` - SystemAutoUpdatesEnabled NumberAsBool `json:"system_AutoUpdate_Enabled"` - SystemAutoUpdatesSecurityPatchesOnlyEnabled NumberAsBool `json:"system_AutoUpdate_SecurityPatchesOnly"` - SystemAutoUpdatesRebootDayOfWeek int64 `json:"system_AutoUpdate_RebootDayOfWeek"` - SystemAutoUpdatesRebootHour int64 `json:"system_AutoUpdate_RebootHour"` - SystemAutoUpdatesRebootMinute int64 `json:"system_AutoUpdate_RebootMinute"` - BackupsEnabled NumberAsBool `json:"backupsActivated"` - RemoteBackupsEnabled NumberAsBool `json:"remoteBackupsActivated"` - ExternalBackupsEnabled NumberAsBool `json:"isExternalBackupActivated"` - ExternalBackupsUpdateDayOfWeek int64 `json:"externalBackupUpdateDay"` - ExternalBackupsUpdateHour int64 `json:"externalBackupUpdateHour"` - ExternalBackupsUpdateMinute int64 `json:"externalBackupUpdateMinute"` - ExternalBackupsUpdateType string `json:"externalBackupUpdateType"` - ExternalBackupsRetainDayOfWeek int64 `json:"externalBackupRetainDay"` - FirewallEnabled NumberAsBool `json:"isFirewallActivated"` - FirewallID string `json:"firewall_id"` - FirewallPorts string `json:"firewallPorts"` + ID string `json:"vmID"` + ProjectID string `json:"projectID"` + ServerName string `json:"displayName"` + ServerType string `json:"serverType"` + TemplateID int64 `json:"template"` + Version string `json:"selected_software_tag"` + ProviderName string `json:"provider"` + Datacenter string `json:"datacenter"` + SupportLevel string `json:"support"` + AdminEmail string `json:"email"` + Category string `json:"category"` + Status string `json:"status"` + DeploymentStatus string `json:"deploymentStatus"` + DeploymentStartedAt string `json:"deploymentStart"` + DeploymentEndedAt string `json:"deploymentEnd"` + CreatorName string `json:"creatorName"` + CreatedAt string `json:"creationDate"` + IPV4 string `json:"ipv4"` + IPV6 string `json:"ipv6"` + CNAME string `json:"cname"` + CustomDomainNames []string `json:"customDomainNames"` + SSHPublicKeys []ServiceSSHPublicKey `json:"sshKeys"` + Country string `json:"country"` + City string `json:"city"` + AdminUser string `json:"adminUser"` + RootAppPath string `json:"rootAppPath"` + GlobalIP string `json:"globalIP"` + AdminExternalPort int64 `json:"adminExternalPort"` + AdminInternalPort int64 `json:"adminInternalPort"` + TrafficOutgoing int64 `json:"traffic_outgoing"` + TrafficIncoming int64 `json:"traffic_ingoing"` + TrafficIncluded int64 `json:"traffic_included"` + Cores int64 `json:"cores"` + RAMSizeGB string `json:"ramGB"` + StorageSizeGB int64 `json:"storageSizeGB"` + PricePerHour string `json:"pricePerHour"` + AppAutoUpdatesEnabled NumberAsBool `json:"app_AutoUpdate_Enabled"` + AppAutoUpdatesDayOfWeek int64 `json:"app_AutoUpdate_DayOfWeek"` + AppAutoUpdatesHour int64 `json:"app_AutoUpdate_Hour"` + AppAutoUpdatesMinute int64 `json:"app_AutoUpdate_Minute"` + SystemAutoUpdatesEnabled NumberAsBool `json:"system_AutoUpdate_Enabled"` + SystemAutoUpdatesSecurityPatchesOnlyEnabled NumberAsBool `json:"system_AutoUpdate_SecurityPatchesOnly"` + SystemAutoUpdatesRebootDayOfWeek int64 `json:"system_AutoUpdate_RebootDayOfWeek"` + SystemAutoUpdatesRebootHour int64 `json:"system_AutoUpdate_RebootHour"` + SystemAutoUpdatesRebootMinute int64 `json:"system_AutoUpdate_RebootMinute"` + BackupsEnabled NumberAsBool `json:"backupsActivated"` + RemoteBackupsEnabled NumberAsBool `json:"remoteBackupsActivated"` + ExternalBackupsEnabled NumberAsBool `json:"isExternalBackupActivated"` + ExternalBackupsUpdateDayOfWeek int64 `json:"externalBackupUpdateDay"` + ExternalBackupsUpdateHour int64 `json:"externalBackupUpdateHour"` + ExternalBackupsUpdateMinute int64 `json:"externalBackupUpdateMinute"` + ExternalBackupsUpdateType string `json:"externalBackupUpdateType"` + ExternalBackupsRetainDayOfWeek int64 `json:"externalBackupRetainDay"` + FirewallEnabled NumberAsBool `json:"isFirewallActivated"` + FirewallID string `json:"firewall_id"` + FirewallPorts string `json:"firewallPorts"` Env map[string]string Admin ServiceAdmin DatabaseAdmin ServiceDatabaseAdmin @@ -613,19 +613,19 @@ func (h *ServiceHandler) RemoveCustomDomainName(serviceId string, domain string) return checkAPIResponse(bts, nil) } -func (h *ServiceHandler) AddSSHKey(serviceId string, keyName string, publicKey string) error { +func (h *ServiceHandler) AddSSHPublicKey(serviceId string, name string, key string) error { req := struct { JWT string `json:"jwt"` ServiceID string `json:"vmID"` Action string `json:"action"` - KeyName string `json:"title"` - PublicKey string `json:"keyHash"` + Name string `json:"name"` + Key string `json:"key"` }{ JWT: h.client.jwt, ServiceID: serviceId, Action: "SSHPubKeysAdd", - KeyName: keyName, - PublicKey: publicKey, + Name: name, + Key: key, } bts, err := h.client.sendPostRequest(fmt.Sprintf("%s/api/servers/DoActionOnServer", h.client.BaseURL), req) @@ -636,17 +636,17 @@ func (h *ServiceHandler) AddSSHKey(serviceId string, keyName string, publicKey s return checkAPIResponse(bts, nil) } -func (h *ServiceHandler) RemoveSSHKey(serviceId string, keyName string) error { +func (h *ServiceHandler) RemoveSSHPublicKey(serviceId string, name string) error { req := struct { JWT string `json:"jwt"` ServiceID string `json:"vmID"` Action string `json:"action"` - KeyName string `json:"deleteParams"` + Name string `json:"deleteParams"` }{ JWT: h.client.jwt, ServiceID: serviceId, Action: "SSHPubKeysRemove", - KeyName: keyName, + Name: name, } bts, err := h.client.sendPostRequest(fmt.Sprintf("%s/api/servers/DoActionOnServer", h.client.BaseURL), req) @@ -856,9 +856,9 @@ func (h *ServiceHandler) GetServiceCustomDomainNames(service *Service) (*[]strin return &customDomainNames, nil } -// GetServiceSSHKeys returns the ssh public keys configured for a service -func (h *ServiceHandler) GetServiceSSHKeys(service *Service) (*[]ServiceSSHKey, error) { - var empty []ServiceSSHKey +// GetServiceSSHPublicKeys returns the ssh public keys configured for a service +func (h *ServiceHandler) GetServiceSSHPublicKeys(service *Service) (*[]ServiceSSHPublicKey, error) { + var empty []ServiceSSHPublicKey if service.DeploymentStatus != ServiceDeploymentStatusDeployed { return &empty, nil @@ -881,7 +881,7 @@ func (h *ServiceHandler) GetServiceSSHKeys(service *Service) (*[]ServiceSSHKey, res := struct { APIResponse - Data []ServiceSSHKey `json:"data"` + Data []ServiceSSHPublicKey `json:"data"` }{} if err := checkAPIResponse(bts, &res); err != nil { @@ -922,11 +922,11 @@ func (h *ServiceHandler) formatServiceForClient(service *Service) (*Service, err } service.CustomDomainNames = *customDomainNames - sshKeys, err := h.GetServiceSSHKeys(service) + sshPublicKeys, err := h.GetServiceSSHPublicKeys(service) if err != nil { - return nil, fmt.Errorf("failed to get service ssh keys: %s", err) + return nil, fmt.Errorf("failed to get service ssh public keys: %s", err) } - service.SSHKeys = *sshKeys + service.SSHPublicKeys = *sshPublicKeys return service, nil } diff --git a/service_test.go b/service_test.go index 5481dc8..318d6e7 100644 --- a/service_test.go +++ b/service_test.go @@ -463,36 +463,36 @@ func TestServiceHandler_RemoveCustomDomain(t *testing.T) { fmt.Fprintf(os.Stdout, "Service: %v", updatedService) } -func TestServiceHandler_AddSSHKey(t *testing.T) { +func TestServiceHandler_AddSSHPublicKey(t *testing.T) { t.Skip("Skipping test") c := setupServiceTestCase(t) - projectID := "3234" - serviceID := "29648534" + projectID := "596" + serviceID := "c4686e74-c75c-4ca8-9aaa-26f83eaaae97" - err := c.Service.AddSSHKey(serviceID, "test", "ssh-rsa fakeKey adam@macbook") + err := c.Service.AddSSHPublicKey(serviceID, "test", "ssh-rsa fakeKey adam@macbook") require.NoError(t, err, "expected no error when adding ssh key") updatedService, err := c.Service.Get(projectID, serviceID) require.NoError(t, err, "expected no error when getting service") - fmt.Fprintf(os.Stdout, "Service: %v", updatedService.SSHKeys) + fmt.Fprintf(os.Stdout, "Service: %v", updatedService.SSHPublicKeys) } -func TestServiceHandler_RemoveSSHKey(t *testing.T) { +func TestServiceHandler_RemoveSSHPublicKey(t *testing.T) { t.Skip("Skipping test") c := setupServiceTestCase(t) - projectID := "3234" - serviceID := "29648534" + projectID := "596" + serviceID := "c4686e74-c75c-4ca8-9aaa-26f83eaaae97" - err := c.Service.RemoveSSHKey(serviceID, "test") + err := c.Service.RemoveSSHPublicKey(serviceID, "test") require.NoError(t, err, "expected no error when removing ssh key") updatedService, err := c.Service.Get(projectID, serviceID) require.NoError(t, err, "expected no error when getting service") - fmt.Fprintf(os.Stdout, "Service: %v", updatedService.SSHKeys) + fmt.Fprintf(os.Stdout, "Service: %v", updatedService.SSHPublicKeys) } func TestServiceHandler_Reboot(t *testing.T) {