|
| 1 | +package instance |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/IBM-Cloud/power-go-client/helpers" |
| 8 | + "github.com/IBM-Cloud/power-go-client/ibmpisession" |
| 9 | + "github.com/IBM-Cloud/power-go-client/power/client/p_cloud_virtual_serial_number" |
| 10 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 11 | +) |
| 12 | + |
| 13 | +// IBMPIVSNClient |
| 14 | + |
| 15 | +type IBMPIVSNClient struct { |
| 16 | + IBMPIClient |
| 17 | +} |
| 18 | + |
| 19 | +// NewIBMPIVSNClient |
| 20 | +func NewIBMPIVSNClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPIVSNClient { |
| 21 | + return &IBMPIVSNClient{ |
| 22 | + *NewIBMPIClient(ctx, sess, cloudInstanceID), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Get Virtual Serial Number |
| 27 | +func (f *IBMPIVSNClient) Get(id string) (*models.VirtualSerialNumber, error) { |
| 28 | + params := p_cloud_virtual_serial_number.NewPcloudVirtualserialnumberGetParams(). |
| 29 | + WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).WithVirtualSerialNumber(id) |
| 30 | + resp, err := f.session.Power.PCloudVirtualSerialNumber.PcloudVirtualserialnumberGet(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 31 | + if err != nil { |
| 32 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get virtual serial number %s :%w", id, err)) |
| 33 | + } |
| 34 | + if resp == nil || resp.Payload == nil { |
| 35 | + return nil, fmt.Errorf("failed to get virtual serial number %s", id) |
| 36 | + } |
| 37 | + return resp.Payload, nil |
| 38 | +} |
| 39 | + |
| 40 | +// Get All Virtual Serial Numbers |
| 41 | +func (f *IBMPIVSNClient) GetAll(pvmInstanceID *string) (models.VirtualSerialNumberList, error) { |
| 42 | + params := p_cloud_virtual_serial_number.NewPcloudVirtualserialnumberGetallParams(). |
| 43 | + WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut) |
| 44 | + if pvmInstanceID != nil { |
| 45 | + params.SetPvmInstanceID(pvmInstanceID) |
| 46 | + } |
| 47 | + resp, err := f.session.Power.PCloudVirtualSerialNumber.PcloudVirtualserialnumberGetall(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 48 | + if err != nil { |
| 49 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get all virtual serial numbers in %s :%w", f.cloudInstanceID, err)) |
| 50 | + } |
| 51 | + if resp == nil || resp.Payload == nil { |
| 52 | + return nil, fmt.Errorf("failed to get all virtual serial numbers in %s", f.cloudInstanceID) |
| 53 | + } |
| 54 | + return resp.Payload, nil |
| 55 | +} |
| 56 | + |
| 57 | +// Update Virtual Serial Nunber |
| 58 | +func (f *IBMPIVSNClient) Update(id string, body *models.UpdateVirtualSerialNumber) (*models.GetServerVirtualSerialNumber, error) { |
| 59 | + params := p_cloud_virtual_serial_number.NewPcloudVirtualserialnumberPutParams(). |
| 60 | + WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).WithVirtualSerialNumber(id). |
| 61 | + WithBody(body) |
| 62 | + resp, err := f.session.Power.PCloudVirtualSerialNumber.PcloudVirtualserialnumberPut(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 63 | + if err != nil { |
| 64 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to update virtual serial number %s :%w", id, err)) |
| 65 | + } |
| 66 | + if resp == nil || resp.Payload == nil { |
| 67 | + return nil, fmt.Errorf("failed to Update virtual serial number %s", id) |
| 68 | + } |
| 69 | + return resp.Payload, nil |
| 70 | +} |
| 71 | + |
| 72 | +// Delete Virtual Serial Number |
| 73 | +func (f *IBMPIVSNClient) Delete(id string) error { |
| 74 | + params := p_cloud_virtual_serial_number.NewPcloudVirtualserialnumberDeleteParams(). |
| 75 | + WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut). |
| 76 | + WithVirtualSerialNumber(id) |
| 77 | + _, err := f.session.Power.PCloudVirtualSerialNumber.PcloudVirtualserialnumberDelete(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 78 | + if err != nil { |
| 79 | + return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete virtual serial number %s :%w", id, err)) |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// PVM Instance Delete VSN |
| 85 | +func (f *IBMPIVSNClient) PVMInstanceDeleteVSN(pvmInstanceID string, body *models.DeleteServerVirtualSerialNumber) error { |
| 86 | + params := p_cloud_virtual_serial_number.NewPcloudPvminstancesVirtualserialnumberDeleteParams(). |
| 87 | + WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithCloudInstanceID(f.cloudInstanceID).WithPvmInstanceID(pvmInstanceID). |
| 88 | + WithBody(body) |
| 89 | + _, err := f.session.Power.PCloudVirtualSerialNumber.PcloudPvminstancesVirtualserialnumberDelete(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 90 | + if err != nil { |
| 91 | + return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete virtual serial number for pvm instance %s :%w", pvmInstanceID, err)) |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// PVM Instance Update VSN |
| 97 | +func (f *IBMPIVSNClient) PVMInstanceUpdateVSN(pvmInstanceID string, body *models.UpdateServerVirtualSerialNumber) (*models.VirtualSerialNumber, error) { |
| 98 | + params := p_cloud_virtual_serial_number.NewPcloudPvminstancesVirtualserialnumberPutParams(). |
| 99 | + WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithCloudInstanceID(f.cloudInstanceID). |
| 100 | + WithPvmInstanceID(pvmInstanceID).WithBody(body) |
| 101 | + resp, err := f.session.Power.PCloudVirtualSerialNumber.PcloudPvminstancesVirtualserialnumberPut(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 102 | + if err != nil { |
| 103 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to update virtual serial number for pvm instance %s :%w", pvmInstanceID, err)) |
| 104 | + } |
| 105 | + if resp == nil || resp.Payload == nil { |
| 106 | + return nil, fmt.Errorf("failed to update virtual serial number for pvm instance %s", pvmInstanceID) |
| 107 | + } |
| 108 | + return resp.Payload, nil |
| 109 | +} |
| 110 | + |
| 111 | +// PVM Attach VSN |
| 112 | +func (f *IBMPIVSNClient) PVMInstanceAttachVSN(pvmInstanceID string, body *models.AddServerVirtualSerialNumber) (*models.VirtualSerialNumber, error) { |
| 113 | + params := p_cloud_virtual_serial_number.NewPcloudPvminstancesVirtualserialnumberPostParams(). |
| 114 | + WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithCloudInstanceID(f.cloudInstanceID). |
| 115 | + WithPvmInstanceID(pvmInstanceID).WithBody(body) |
| 116 | + resp, err := f.session.Power.PCloudVirtualSerialNumber.PcloudPvminstancesVirtualserialnumberPost(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 117 | + if err != nil { |
| 118 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to attach virtual serial number for pvm instance %s :%w", pvmInstanceID, err)) |
| 119 | + } |
| 120 | + if resp == nil || resp.Payload == nil { |
| 121 | + return nil, fmt.Errorf("failed to attach virtual serial number for pvm instance %s", pvmInstanceID) |
| 122 | + } |
| 123 | + return resp.Payload, nil |
| 124 | +} |
0 commit comments