Skip to content

Commit

Permalink
feat: add api RenameHostById
Browse files Browse the repository at this point in the history
  • Loading branch information
Meng-20 committed Oct 3, 2024
1 parent 013976a commit 06aba1c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
41 changes: 40 additions & 1 deletion api/cfm-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ paths:
# Rename Blade
/cfm/v1/appliances/{applianceId}/blades/{bladeId}/rename:
post:
description: Rename a memory blade by id.
description: Rename a blade by id.
operationId: bladesRenameById
parameters:
- $ref: "#/components/parameters/applianceId"
Expand Down Expand Up @@ -885,6 +885,45 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/statusMessage"

# Rename CXL Host
/cfm/v1/hosts/{hostId}/rename:
post:
description: Rename a CXL Host by id.
operationId: hostsRenameById
parameters:
- $ref: "#/components/parameters/hostId"
- name: newHostId
in: query
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/blade'
"400":
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"404":
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: "#/components/schemas/statusMessage"

# CXL Host Ports
/cfm/v1/hosts/{hostId}/ports:
get:
Expand Down
26 changes: 25 additions & 1 deletion pkg/api/api_default_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func (cfm *CfmApiService) BladesGetPorts(ctx context.Context, applianceId string
}

// BladesRenameById -
func (s *CfmApiService) BladesRenameById(ctx context.Context, applianceId string, bladeId string, newBladeId string) (openapi.ImplResponse, error) {
func (cfm *CfmApiService) BladesRenameById(ctx context.Context, applianceId string, bladeId string, newBladeId string) (openapi.ImplResponse, error) {
appliance, err := manager.GetApplianceById(ctx, applianceId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
Expand Down Expand Up @@ -994,6 +994,30 @@ func (cfm *CfmApiService) HostsPost(ctx context.Context, credentials openapi.Cre
return openapi.Response(http.StatusCreated, h), nil
}

// HostsRenameById -
func (cfm *CfmApiService) HostsRenameById(ctx context.Context, hostId string, newHostId string) (openapi.ImplResponse, error) {
// Make sure the hostId exists
// Get the host information from the manager level and is used for renaming
host, err := manager.GetHostById(ctx, hostId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

// Make sure the newHostId doesn't exist
existHost, err := manager.GetHostById(ctx, newHostId)
if existHost != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

//Rename the cxl host with the new id
newHost, err := manager.RenameHost(ctx, host, newHostId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

return openapi.Response(http.StatusOK, newHost), nil
}

// HostsResync -
func (cfm *CfmApiService) HostsResyncById(ctx context.Context, hostId string) (openapi.ImplResponse, error) {
host, err := manager.ResyncHostById(ctx, hostId)
Expand Down
28 changes: 28 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,34 @@ func AddHost(ctx context.Context, c *openapi.Credentials) (*Host, error) {
return host, nil
}

func RenameHost(ctx context.Context, host *Host, newHostId string) (*Host, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> RenameHostById: ", "hostId", host.Id)
// Save the host credentials for adding back with the new name
c := &openapi.Credentials{
Username: host.creds.Username,
Password: host.creds.Password,
IpAddress: host.creds.IpAddress,
Port: host.creds.Port,
Insecure: host.creds.Insecure,
Protocol: host.creds.Protocol,
CustomId: newHostId,
}

// delete host
_, err := DeleteHostById(ctx, host.Id)
if err != nil {
return nil, err.(*common.RequestError)
}

// Add the host back with the new name
newHost, err := AddHost(ctx, c)
if err != nil {
return nil, err.(*common.RequestError)
}
return newHost, nil
}

func DeleteHostById(ctx context.Context, hostId string) (*Host, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> DeleteHostById: ", "hostId", hostId)
Expand Down

0 comments on commit 06aba1c

Please sign in to comment.