diff --git a/cloud/template.go b/cloud/template.go index 1590fcd..3081386 100644 --- a/cloud/template.go +++ b/cloud/template.go @@ -128,7 +128,7 @@ func templateString(indent, ljust int, delim, key string, value any) string { for _, template := range templates { - buf.WriteString(fmt.Sprintf("%s%s = [\n", strings.Repeat("\t", indent), pad(key, ljust))) + fmt.Fprintf(&buf, "%s%s = [\n", strings.Repeat("\t", indent), pad(key, ljust)) kps := make([]keyValuePair, 0, len(template)) length := 0 @@ -152,7 +152,7 @@ func templateString(indent, ljust int, delim, key string, value any) string { buf.WriteString(templateString(indent+1, length, delim, pad(kps[i].key, length), kps[i].value)) } - buf.WriteString(fmt.Sprintf("%s]\n", strings.Repeat("\t", indent))) + fmt.Fprintf(&buf, "%s]\n", strings.Repeat("\t", indent)) } @@ -259,7 +259,7 @@ func newTemplate(t any) map[string]any { //nolint:gocognit } func simpleValue(v reflect.Value) any { - if v.Kind() == reflect.Ptr { + if v.Kind() == reflect.Pointer { if v.IsNil() { return nil } diff --git a/deprecated/v2/cloud/template.go b/deprecated/v2/cloud/template.go index a84519e..e40fa20 100644 --- a/deprecated/v2/cloud/template.go +++ b/deprecated/v2/cloud/template.go @@ -114,7 +114,7 @@ func templateString(indent, ljust int, delim, key string, value any) string { for _, template := range templates { - buf.WriteString(fmt.Sprintf("%s%s = [\n", strings.Repeat("\t", indent), pad(key, ljust))) + fmt.Fprintf(&buf, "%s%s = [\n", strings.Repeat("\t", indent), pad(key, ljust)) kps := make([]keyValuePair, 0, len(template)) length := 0 @@ -138,7 +138,7 @@ func templateString(indent, ljust int, delim, key string, value any) string { buf.WriteString(templateString(indent+1, length, delim, pad(kps[i].key, length), kps[i].value)) } - buf.WriteString(fmt.Sprintf("%s]\n", strings.Repeat("\t", indent))) + fmt.Fprintf(&buf, "%s]\n", strings.Repeat("\t", indent)) } diff --git a/deprecated/v2/snapshot/auto.go b/deprecated/v2/snapshot/auto.go index d634cb2..c2c16c4 100644 --- a/deprecated/v2/snapshot/auto.go +++ b/deprecated/v2/snapshot/auto.go @@ -16,56 +16,56 @@ type AutoRequest struct { // ID return the ID of the request. func (a AutoRequest) ID() uint { - return a.requestBase.id + return a.id } // Scheduled returns the time the request was scheduled. func (a AutoRequest) Scheduled() time.Time { - return a.requestBase.scheduled + return a.scheduled } // Started returns the time the request was started. func (a AutoRequest) Started() time.Time { - return a.requestBase.started + return a.started } // Finished returns the time the request was finished. func (a AutoRequest) Finished() time.Time { - return a.requestBase.finished + return a.finished } // Schedule returns the request's snapshot schedule. func (a AutoRequest) Schedule() Schedule { - return a.requestBase.schedule + return a.schedule } // SetID sets the ID of the request. func (a *AutoRequest) SetID(id uint) Request { - a.requestBase.id = id + a.id = id return a } // SetScheduled sets the scheduled time of the request. func (a *AutoRequest) SetScheduled(t time.Time) Request { - a.requestBase.scheduled = t + a.scheduled = t return a } // SetStarted sets the started time of the request. func (a *AutoRequest) SetStarted(t time.Time) Request { - a.requestBase.started = t + a.started = t return a } // SetFinished sets the finished time of the request. func (a *AutoRequest) SetFinished(t time.Time) Request { - a.requestBase.finished = t + a.finished = t return a } // SetSchedule sets the request's snapshot schedule. func (a *AutoRequest) SetSchedule(s Schedule) Request { - a.requestBase.schedule = s + a.schedule = s return a } @@ -80,8 +80,8 @@ func (a AutoRequest) VMID() int { // Tag returns the Tag that will be used when processing the request. func (a AutoRequest) Tag() Tag { tag := Tag{ - Period: a.requestBase.schedule.Period(), - Time: a.requestBase.scheduled, + Period: a.schedule.Period(), + Time: a.scheduled, VM: -1, Version: 2, } diff --git a/deprecated/v2/snapshot/manual.go b/deprecated/v2/snapshot/manual.go index e1378e0..9e140ac 100644 --- a/deprecated/v2/snapshot/manual.go +++ b/deprecated/v2/snapshot/manual.go @@ -12,56 +12,56 @@ type ManualRequest struct { // ID return the ID of the request. func (m ManualRequest) ID() uint { - return m.requestBase.id + return m.id } // Scheduled returns the time the request was scheduled. func (m ManualRequest) Scheduled() time.Time { - return m.requestBase.scheduled + return m.scheduled } // Started returns the time the request was started. func (m ManualRequest) Started() time.Time { - return m.requestBase.started + return m.started } // Finished returns the time the request was finished. func (m ManualRequest) Finished() time.Time { - return m.requestBase.finished + return m.finished } // Schedule returns the request's snapshot schedule. func (m ManualRequest) Schedule() Schedule { - return m.requestBase.schedule + return m.schedule } // SetID sets the ID of the request. func (m *ManualRequest) SetID(id uint) Request { - m.requestBase.id = id + m.id = id return m } // SetScheduled sets the scheduled time of the request. func (m *ManualRequest) SetScheduled(t time.Time) Request { - m.requestBase.scheduled = t + m.scheduled = t return m } // SetStarted sets the started time of the request. func (m *ManualRequest) SetStarted(t time.Time) Request { - m.requestBase.started = t + m.started = t return m } // SetFinished sets the finished time of the request. func (m *ManualRequest) SetFinished(t time.Time) Request { - m.requestBase.finished = t + m.finished = t return m } // SetSchedule sets the request's snapshot schedule. func (m *ManualRequest) SetSchedule(s Schedule) Request { - m.requestBase.schedule = s + m.schedule = s return m } @@ -71,8 +71,8 @@ func (m ManualRequest) VMID() int { return m.VM } // Tag returns the Tag that will be used when processing the request. func (m ManualRequest) Tag() Tag { return Tag{ - Period: m.requestBase.schedule.Period(), - Time: m.requestBase.scheduled, + Period: m.schedule.Period(), + Time: m.scheduled, RBD: m.RBDName, Version: 3, } diff --git a/deprecated/v2/snapshot/schedule.go b/deprecated/v2/snapshot/schedule.go index dfa4397..e8be722 100644 --- a/deprecated/v2/snapshot/schedule.go +++ b/deprecated/v2/snapshot/schedule.go @@ -816,7 +816,7 @@ func (s *Schedule) setHourly(when string) error { return err } - if !(minute >= 0 && minute <= 59) { + if minute < 0 || minute > 59 { return fmt.Errorf("invalid minute %d", minute) } @@ -845,7 +845,7 @@ func (s *Schedule) setDaily(when string) error { return err } - if !(hour >= 0 && hour <= 23) { + if hour < 0 || hour > 23 { return fmt.Errorf("invalid hour %d", hour) } @@ -884,7 +884,7 @@ func (s *Schedule) setWeekly(when string) error { } } - if !(day >= time.Sunday && day <= time.Saturday) { + if day < time.Sunday || day > time.Saturday { return fmt.Errorf("invalid day %v", day) } @@ -913,7 +913,7 @@ func (s *Schedule) setMonthly(when string) error { return err } - if !(day >= 1 && day <= 31) { + if day < 1 || day > 31 { return fmt.Errorf("invalid day %d", day) } @@ -971,7 +971,7 @@ func (s *Schedule) setYearly(when string) error { } if month != 0 { - if !(month >= time.January && month <= time.December) { + if month < time.January || month > time.December { return fmt.Errorf("invalid month %v", month) } @@ -981,7 +981,7 @@ func (s *Schedule) setYearly(when string) error { } if yearday != 0 { - if !(yearday >= 1 && yearday <= 366) { + if yearday < 1 || yearday > 366 { return fmt.Errorf("invalid yearday %d", yearday) } diff --git a/fed/fed.go b/fed/fed.go new file mode 100644 index 0000000..0da085b --- /dev/null +++ b/fed/fed.go @@ -0,0 +1,26 @@ +// Package fed holds types and services for the federation endpoints. +package fed + +// PathPrefix is the root path for federation API endpoints. +const PathPrefix = "fed" + +// FederateInfoPath is the path for get-federate-info endpoint. +const FederateInfoPath = "get-federate-info" + +// FederationPath is the path for get-federation endpoint. +const FederationPath = "get-federation" + +// RemoveFederatePath is the path for remove-federate endpoint. +const RemoveFederatePath = "remove-federate" + +// AddNewFederatePath is the path for add-new-federate endpoint. +const AddNewFederatePath = "add-new-federate" + +// AuthorizeFederationPath is the path for authorize-federation endpoint. +const AuthorizeFederationPath = "authorize-federation" + +// ClusterOverviewPath is the path for cluster/overview endpoint. +const ClusterOverviewPath = "cluster/overview" + +// FederateLoginPath is the path for federate-login endpoint. +const FederateLoginPath = "federate-login" diff --git a/fed/service_fed.go b/fed/service_fed.go new file mode 100644 index 0000000..aa9a8b9 --- /dev/null +++ b/fed/service_fed.go @@ -0,0 +1,67 @@ +package fed + +import ( + "context" + + "github.com/softiron/manifold-api/client" + "github.com/softiron/manifold-api/internal/api" +) + +// Service owns the /fed methods. +type Service struct { + *service +} + +type service struct { + *client.Client + root string +} + +// NewService returns a new Service for federation operations. +func NewService(c *client.Client, path string) *Service { + s := &service{Client: c, root: path} + + return &Service{ + service: s, + } +} + +// path returns a URL path with the correct prefix appended. +func (s service) path(dirs ...interface{}) string { + return api.Path(s.root, PathPrefix, dirs...) +} + +// GetFederateInfo returns federate information. +func (s Service) GetFederateInfo(ctx context.Context) error { + p := s.path(FederateInfoPath) + + return s.Get(ctx, p, nil) +} + +// GetFederation returns federation information. +func (s Service) GetFederation(ctx context.Context) error { + p := s.path(FederationPath) + + return s.Get(ctx, p, nil) +} + +// RemoveFederate removes a federate. +func (s Service) RemoveFederate(ctx context.Context) error { + p := s.path(RemoveFederatePath) + + return s.Post(ctx, p, nil, nil) +} + +// AddNewFederate adds a new federate. +func (s Service) AddNewFederate(ctx context.Context) error { + p := s.path(AddNewFederatePath) + + return s.Post(ctx, p, nil, nil) +} + +// AuthorizeFederation authorizes federation. +func (s Service) AuthorizeFederation(ctx context.Context) error { + p := s.path(AuthorizeFederationPath) + + return s.Post(ctx, p, nil, nil) +} diff --git a/management/management.go b/management/management.go new file mode 100644 index 0000000..b153b70 --- /dev/null +++ b/management/management.go @@ -0,0 +1,35 @@ +// Package management holds types and services for the /v3-preview/management endpoints. +package management + +// PathPrefix is the root path for management API endpoints. +const PathPrefix = "management" + +// FactPath is the path for fact-related endpoints. +const FactPath = "fact" + +// ClusterNamePath is the path for cluster-name endpoint. +const ClusterNamePath = "cluster-name" + +// SSHBannerPath is the path for ssh-banner endpoint. +const SSHBannerPath = "ssh-banner" + +// LicensePath is the path for license endpoint. +const LicensePath = "license" + +// StorageVLANPath is the path for storage-vlan endpoint. +const StorageVLANPath = "storage-vlan" + +// ComputeVLANPath is the path for compute-vlan endpoint. +const ComputeVLANPath = "compute-vlan" + +// DashboardNetworkPath is the path for dashboard-network endpoint. +const DashboardNetworkPath = "dashboard-network" + +// DashboardVLANPath is the path for dashboard-vlan endpoint. +const DashboardVLANPath = "dashboard-vlan" + +// TopologyPath is the path for topology endpoint. +const TopologyPath = "topology" + +// SSHPath is the path for ssh keys endpoint. +const SSHPath = "ssh" diff --git a/management/management_types.go b/management/management_types.go new file mode 100644 index 0000000..188bab2 --- /dev/null +++ b/management/management_types.go @@ -0,0 +1,105 @@ +package management + +// FactListResponse is the response for the list facts endpoint. +type FactListResponse struct { + Facts []FactInfo `json:"facts"` +} + +// FactInfo describes a single fact accessible via the generic endpoint. +type FactInfo struct { + ID string `json:"id"` + Name string `json:"name"` + ReadOnly bool `json:"readonly"` + Required bool `json:"required"` + Type string `json:"type"` + Value string `json:"value"` +} + +// FactGetResponse is the response for the get fact endpoint. +type FactGetResponse struct { + ID string `json:"id"` + Value string `json:"value"` +} + +// RequestSetFact is the request to set a fact value in SiControlD. +type RequestSetFact struct { + Value string `json:"value"` +} + +// ResponseGetFact is the response to get a fact value from SiControlD. +type ResponseGetFact struct { + Value string `json:"value"` +} + +// DashboardNetworkConfig holds the network configuration for the dashboard VM. +type DashboardNetworkConfig struct { + Address string `json:"address"` + Netmask string `json:"netmask"` + Gateway string `json:"gateway"` + DNS string `json:"dns"` + Syslog string `json:"syslog"` + NTP string `json:"ntp"` +} + +// UserDatastore holds the datastore configuration for a user. +type UserDatastore struct { + Disk string `json:"disk"` + TotalSize int64 `json:"total_size"` + UsedSize int64 `json:"used_size"` +} + +// UserNetwork holds the network configuration for a user. +type UserNetwork struct { + Network int `json:"network"` + Leases int `json:"leases"` + Used int `json:"used"` + Free int `json:"free"` +} + +// UserInstance holds the instance configuration for a user. +type UserInstance struct { + Instances int64 `json:"instances"` + Used int64 `json:"used"` + Limit int64 `json:"limit"` +} + +// UserImage holds the image configuration for a user. +type UserImage struct { + Images int64 `json:"images"` + Used int64 `json:"used"` + DiskSize int64 `json:"disk_size"` + TotalSize int64 `json:"total_size"` + Available int64 `json:"available"` +} + +// UserDefaultQuotas holds the default quotas for a user. +type UserDefaultQuotas struct { + Datastore UserDatastore `json:"datastore"` + Network UserNetwork `json:"network"` + Image UserImage `json:"image"` + Instance UserInstance `json:"instance"` +} + +// SSHKeysResponse contains a list of SSH keys. +type SSHKeysResponse struct { + Keys []string `json:"keys"` +} + +// SSHKeyRequest is the request for the add and delete SSH key endpoints. +type SSHKeyRequest struct { + Key string `json:"key"` +} + +// SetPasswordRequest is the request to change a user's password. +type SetPasswordRequest struct { + Password string `json:"password"` +} + +// ClusterTopology holds the cluster topology configuration. +type ClusterTopology struct { + ComputeVlan uint `json:"compute_vlan"` + StorageVlan uint `json:"storage_vlan"` + DashboardVlan uint `json:"dashboard_vlan"` + CommonVlan uint `json:"common_vlan"` + DatacenterVlan uint `json:"datacenter_vlan"` +} diff --git a/management/service_management.go b/management/service_management.go new file mode 100644 index 0000000..7547c37 --- /dev/null +++ b/management/service_management.go @@ -0,0 +1,263 @@ +package management + +import ( + "context" + + "github.com/softiron/manifold-api/client" + "github.com/softiron/manifold-api/internal/api" +) + +// Service owns the /management methods. +type Service struct { + *service +} + +type service struct { + *client.Client + root string +} + +// NewService returns a new Service for management operations. +func NewService(c *client.Client, path string) *Service { + s := &service{Client: c, root: path} + + return &Service{ + service: s, + } +} + +// path returns a URL path with the correct prefix appended. +func (s service) path(dirs ...interface{}) string { + return api.Path(s.root, PathPrefix, dirs...) +} + +// ListFacts returns a list of all generic form management facts. +func (s Service) ListFacts(ctx context.Context) (*FactListResponse, error) { + var resp FactListResponse + + p := s.path(FactPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// GetFact returns the current value of the specified fact. +func (s Service) GetFact(ctx context.Context, factID string) (*FactGetResponse, error) { + var resp FactGetResponse + + p := s.path(FactPath, factID) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetFact sets the value of the specified fact. +func (s Service) SetFact(ctx context.Context, factID, value string) error { + p := s.path(FactPath, factID) + + req := RequestSetFact{Value: value} + + return s.Put(ctx, p, req, nil) +} + +// GetClusterName gets the name of this cluster. +func (s Service) GetClusterName(ctx context.Context) (*ResponseGetFact, error) { + var resp ResponseGetFact + + p := s.path(ClusterNamePath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetClusterName sets the name of this cluster. +func (s Service) SetClusterName(ctx context.Context, name string) error { + p := s.path(ClusterNamePath) + + req := RequestSetFact{Value: name} + + return s.Put(ctx, p, req, nil) +} + +// GetSSHBanner gets the SSH banner text. +func (s Service) GetSSHBanner(ctx context.Context) (*ResponseGetFact, error) { + var resp ResponseGetFact + + p := s.path(SSHBannerPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetSSHBanner sets the SSH banner text. +func (s Service) SetSSHBanner(ctx context.Context, banner string) error { + p := s.path(SSHBannerPath) + + req := RequestSetFact{Value: banner} + + return s.Put(ctx, p, req, nil) +} + +// GetLicense gets the license text. +func (s Service) GetLicense(ctx context.Context) (*ResponseGetFact, error) { + var resp ResponseGetFact + + p := s.path(LicensePath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetLicense sets the license text. +func (s Service) SetLicense(ctx context.Context, license string) error { + p := s.path(LicensePath) + + req := RequestSetFact{Value: license} + + return s.Put(ctx, p, req, nil) +} + +// GetStorageVlan gets the storage VLAN ID. +func (s Service) GetStorageVlan(ctx context.Context) (*ResponseGetFact, error) { + var resp ResponseGetFact + + p := s.path(StorageVLANPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetStorageVlan sets the storage VLAN ID. +func (s Service) SetStorageVlan(ctx context.Context, vlan string) error { + p := s.path(StorageVLANPath) + + req := RequestSetFact{Value: vlan} + + return s.Put(ctx, p, req, nil) +} + +// GetComputeVlan gets the compute VLAN ID. +func (s Service) GetComputeVlan(ctx context.Context) (*ResponseGetFact, error) { + var resp ResponseGetFact + + p := s.path(ComputeVLANPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetComputeVlan sets the compute VLAN ID. +func (s Service) SetComputeVlan(ctx context.Context, vlan string) error { + p := s.path(ComputeVLANPath) + + req := RequestSetFact{Value: vlan} + + return s.Put(ctx, p, req, nil) +} + +// GetDashboardNetwork gets the dashboard VM network configuration. +func (s Service) GetDashboardNetwork(ctx context.Context) (*DashboardNetworkConfig, error) { + var resp DashboardNetworkConfig + + p := s.path(DashboardNetworkPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetDashboardNetwork sets the dashboard VM network configuration. +func (s Service) SetDashboardNetwork(ctx context.Context, config DashboardNetworkConfig) error { + p := s.path(DashboardNetworkPath) + + return s.Put(ctx, p, config, nil) +} + +// GetDashboardVlan gets the dashboard VLAN ID. +func (s Service) GetDashboardVlan(ctx context.Context) (*ResponseGetFact, error) { + var resp ResponseGetFact + + p := s.path(DashboardVLANPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// SetDashboardVlan sets the dashboard VLAN ID. +func (s Service) SetDashboardVlan(ctx context.Context, vlan string) error { + p := s.path(DashboardVLANPath) + + req := RequestSetFact{Value: vlan} + + return s.Put(ctx, p, req, nil) +} + +// GetTopology gets the topology configuration. +func (s Service) GetTopology(ctx context.Context) (*ClusterTopology, error) { + var resp ClusterTopology + + p := s.path(TopologyPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// ListSSHKeys gets a list the authorized SSH public keys. +func (s Service) ListSSHKeys(ctx context.Context) (*SSHKeysResponse, error) { + var resp SSHKeysResponse + + p := s.path(SSHPath) + + if err := s.Get(ctx, p, &resp); err != nil { + return nil, err + } + + return &resp, nil +} + +// AddSSHKey adds the given SSH public key to authorized list. +func (s Service) AddSSHKey(ctx context.Context, key string) error { + p := s.path(SSHPath) + + req := SSHKeyRequest{Key: key} + + return s.Put(ctx, p, req, nil) +} + +// DeleteSSHKey removes the given SSH public key from authorized list. +func (s Service) DeleteSSHKey(ctx context.Context, key string) error { + p := s.path(SSHPath) + + req := SSHKeyRequest{Key: key} + + return s.Delete(ctx, p, req) +} diff --git a/manifold/client.go b/manifold/client.go index c026590..67e066b 100644 --- a/manifold/client.go +++ b/manifold/client.go @@ -9,6 +9,8 @@ import ( "github.com/softiron/manifold-api/client" "github.com/softiron/manifold-api/cloud" + "github.com/softiron/manifold-api/fed" + "github.com/softiron/manifold-api/management" "github.com/softiron/manifold-api/metal" "github.com/softiron/manifold-api/snapshot" "github.com/softiron/manifold-api/upload" @@ -16,10 +18,12 @@ import ( // Client is a connection to the manifold-api service. type Client struct { - Cloud *cloud.Service - Metal *metal.Service - Upload *upload.Service - Snapshot *snapshot.Service + Cloud *cloud.Service + Metal *metal.Service + Upload *upload.Service + Snapshot *snapshot.Service + Management *management.Service + Fed *fed.Service *client.Client } @@ -61,11 +65,13 @@ func NewClient(o *client.Options, fn ...func(*client.Client)) *Client { prefix := APIPrefix + "/" + APIVersion return &Client{ - Cloud: cloud.NewService(c, prefix), - Metal: metal.NewService(c, prefix), - Upload: upload.NewService(c, prefix), - Snapshot: snapshot.NewService(c, prefix), - Client: c, + Cloud: cloud.NewService(c, prefix), + Metal: metal.NewService(c, prefix), + Upload: upload.NewService(c, prefix), + Snapshot: snapshot.NewService(c, prefix), + Management: management.NewService(c, prefix), + Fed: fed.NewService(c, prefix), + Client: c, } } diff --git a/snapshot/auto.go b/snapshot/auto.go index 75a8ac4..bae6eec 100644 --- a/snapshot/auto.go +++ b/snapshot/auto.go @@ -17,56 +17,56 @@ type AutoRequest struct { // ID return the ID of the request. func (a AutoRequest) ID() uint { - return a.requestBase.id + return a.id } // Scheduled returns the time the request was scheduled. func (a AutoRequest) Scheduled() time.Time { - return a.requestBase.scheduled + return a.scheduled } // Started returns the time the request was started. func (a AutoRequest) Started() time.Time { - return a.requestBase.started + return a.started } // Finished returns the time the request was finished. func (a AutoRequest) Finished() time.Time { - return a.requestBase.finished + return a.finished } // Schedule returns the request's snapshot schedule. func (a AutoRequest) Schedule() Schedule { - return a.requestBase.schedule + return a.schedule } // SetID sets the ID of the request. func (a *AutoRequest) SetID(id uint) Request { - a.requestBase.id = id + a.id = id return a } // SetScheduled sets the scheduled time of the request. func (a *AutoRequest) SetScheduled(t time.Time) Request { - a.requestBase.scheduled = t + a.scheduled = t return a } // SetStarted sets the started time of the request. func (a *AutoRequest) SetStarted(t time.Time) Request { - a.requestBase.started = t + a.started = t return a } // SetFinished sets the finished time of the request. func (a *AutoRequest) SetFinished(t time.Time) Request { - a.requestBase.finished = t + a.finished = t return a } // SetSchedule sets the request's snapshot schedule. func (a *AutoRequest) SetSchedule(s Schedule) Request { - a.requestBase.schedule = s + a.schedule = s return a } @@ -81,8 +81,8 @@ func (a AutoRequest) VMID() int { // Tag returns the Tag that will be used when processing the request. func (a AutoRequest) Tag() Tag { tag := Tag{ - Period: a.requestBase.schedule.Period(), - Time: a.requestBase.scheduled, + Period: a.schedule.Period(), + Time: a.scheduled, VM: -1, Version: 2, } diff --git a/snapshot/manual.go b/snapshot/manual.go index e1378e0..9e140ac 100644 --- a/snapshot/manual.go +++ b/snapshot/manual.go @@ -12,56 +12,56 @@ type ManualRequest struct { // ID return the ID of the request. func (m ManualRequest) ID() uint { - return m.requestBase.id + return m.id } // Scheduled returns the time the request was scheduled. func (m ManualRequest) Scheduled() time.Time { - return m.requestBase.scheduled + return m.scheduled } // Started returns the time the request was started. func (m ManualRequest) Started() time.Time { - return m.requestBase.started + return m.started } // Finished returns the time the request was finished. func (m ManualRequest) Finished() time.Time { - return m.requestBase.finished + return m.finished } // Schedule returns the request's snapshot schedule. func (m ManualRequest) Schedule() Schedule { - return m.requestBase.schedule + return m.schedule } // SetID sets the ID of the request. func (m *ManualRequest) SetID(id uint) Request { - m.requestBase.id = id + m.id = id return m } // SetScheduled sets the scheduled time of the request. func (m *ManualRequest) SetScheduled(t time.Time) Request { - m.requestBase.scheduled = t + m.scheduled = t return m } // SetStarted sets the started time of the request. func (m *ManualRequest) SetStarted(t time.Time) Request { - m.requestBase.started = t + m.started = t return m } // SetFinished sets the finished time of the request. func (m *ManualRequest) SetFinished(t time.Time) Request { - m.requestBase.finished = t + m.finished = t return m } // SetSchedule sets the request's snapshot schedule. func (m *ManualRequest) SetSchedule(s Schedule) Request { - m.requestBase.schedule = s + m.schedule = s return m } @@ -71,8 +71,8 @@ func (m ManualRequest) VMID() int { return m.VM } // Tag returns the Tag that will be used when processing the request. func (m ManualRequest) Tag() Tag { return Tag{ - Period: m.requestBase.schedule.Period(), - Time: m.requestBase.scheduled, + Period: m.schedule.Period(), + Time: m.scheduled, RBD: m.RBDName, Version: 3, } diff --git a/snapshot/schedule.go b/snapshot/schedule.go index dfa4397..e8be722 100644 --- a/snapshot/schedule.go +++ b/snapshot/schedule.go @@ -816,7 +816,7 @@ func (s *Schedule) setHourly(when string) error { return err } - if !(minute >= 0 && minute <= 59) { + if minute < 0 || minute > 59 { return fmt.Errorf("invalid minute %d", minute) } @@ -845,7 +845,7 @@ func (s *Schedule) setDaily(when string) error { return err } - if !(hour >= 0 && hour <= 23) { + if hour < 0 || hour > 23 { return fmt.Errorf("invalid hour %d", hour) } @@ -884,7 +884,7 @@ func (s *Schedule) setWeekly(when string) error { } } - if !(day >= time.Sunday && day <= time.Saturday) { + if day < time.Sunday || day > time.Saturday { return fmt.Errorf("invalid day %v", day) } @@ -913,7 +913,7 @@ func (s *Schedule) setMonthly(when string) error { return err } - if !(day >= 1 && day <= 31) { + if day < 1 || day > 31 { return fmt.Errorf("invalid day %d", day) } @@ -971,7 +971,7 @@ func (s *Schedule) setYearly(when string) error { } if month != 0 { - if !(month >= time.January && month <= time.December) { + if month < time.January || month > time.December { return fmt.Errorf("invalid month %v", month) } @@ -981,7 +981,7 @@ func (s *Schedule) setYearly(when string) error { } if yearday != 0 { - if !(yearday >= 1 && yearday <= 366) { + if yearday < 1 || yearday > 366 { return fmt.Errorf("invalid yearday %d", yearday) } diff --git a/upload/service.go b/upload/service.go index e3a7c94..c79504e 100644 --- a/upload/service.go +++ b/upload/service.go @@ -55,7 +55,7 @@ func (s Service) Create(ctx context.Context, path string) (*CreateResponse, erro header[uploadLength] = strconv.FormatInt(stat.Size(), 10) header[uploadMetadata] = "filename " + base64.StdEncoding.EncodeToString([]byte(stat.Name())) - resp, err := s.Client.Request(ctx, client.AccessTokenAuth, http.MethodPost, s.path(), header, http.NoBody) + resp, err := s.Request(ctx, client.AccessTokenAuth, http.MethodPost, s.path(), header, http.NoBody) if err != nil { return nil, err } @@ -162,7 +162,7 @@ func (s Service) Resume(ctx context.Context, id, path string) (*ResumeResponse, // be used to resume the upload. func (s Service) Status(ctx context.Context, path string) (*StatusResponse, error) { header := newClientHeader() - resp, err := s.Client.Request(ctx, client.AccessTokenAuth, http.MethodHead, s.path(path), header, http.NoBody) + resp, err := s.Request(ctx, client.AccessTokenAuth, http.MethodHead, s.path(path), header, http.NoBody) if err != nil { return nil, err } @@ -189,7 +189,7 @@ func (s Service) Status(ctx context.Context, path string) (*StatusResponse, erro // Info returns information about the server's capabilities. func (s Service) Info(ctx context.Context) (*InfoResponse, error) { - resp, err := s.Client.Request(ctx, client.AccessTokenAuth, http.MethodOptions, s.path(), nil, http.NoBody) + resp, err := s.Request(ctx, client.AccessTokenAuth, http.MethodOptions, s.path(), nil, http.NoBody) if err != nil { return nil, err } @@ -242,7 +242,7 @@ func (s Service) upload(ctx context.Context, id string, offset int64, r io.ReadS header[uploadOffset] = strconv.FormatInt(offset, 10) header["Content-Length"] = strconv.Itoa(n) - resp, err := s.Client.Request(ctx, client.AccessTokenAuth, http.MethodPatch, s.path(id), header, bytes.NewReader(b[:n])) + resp, err := s.Request(ctx, client.AccessTokenAuth, http.MethodPatch, s.path(id), header, bytes.NewReader(b[:n])) if err != nil { return nil, err }