|
| 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/network_security_groups" |
| 10 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 11 | +) |
| 12 | + |
| 13 | +// IBMPINetworkSecurityGroupClient |
| 14 | +type IBMPINetworkSecurityGroupClient struct { |
| 15 | + IBMPIClient |
| 16 | +} |
| 17 | + |
| 18 | +// NewIBMIPINetworkSecurityGroupClient |
| 19 | +func NewIBMIPINetworkSecurityGroupClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPINetworkSecurityGroupClient { |
| 20 | + return &IBMPINetworkSecurityGroupClient{ |
| 21 | + *NewIBMPIClient(ctx, sess, cloudInstanceID), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Get a network security group |
| 26 | +func (f *IBMPINetworkSecurityGroupClient) Get(id string) (*models.NetworkSecurityGroup, error) { |
| 27 | + params := network_security_groups.NewV1NetworkSecurityGroupsIDGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).WithNetworkSecurityGroupID(id) |
| 28 | + resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsIDGet(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 29 | + if err != nil { |
| 30 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network security group %s: %w", id, err)) |
| 31 | + } |
| 32 | + if resp == nil || resp.Payload == nil { |
| 33 | + return nil, fmt.Errorf("failed to get network security group %s", id) |
| 34 | + } |
| 35 | + return resp.Payload, nil |
| 36 | +} |
| 37 | + |
| 38 | +// Get all network security groups |
| 39 | +func (f *IBMPINetworkSecurityGroupClient) GetAll() (*models.NetworkSecurityGroups, error) { |
| 40 | + params := network_security_groups.NewV1NetworkSecurityGroupsListParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut) |
| 41 | + resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsList(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 42 | + if err != nil { |
| 43 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network security groups %s", err)) |
| 44 | + } |
| 45 | + if resp == nil || resp.Payload == nil { |
| 46 | + return nil, fmt.Errorf("failed to get network security groups") |
| 47 | + } |
| 48 | + return resp.Payload, nil |
| 49 | +} |
| 50 | + |
| 51 | +// Create a network security group |
| 52 | +func (f *IBMPINetworkSecurityGroupClient) Create(body *models.NetworkSecurityGroupCreate) (*models.NetworkSecurityGroup, error) { |
| 53 | + params := network_security_groups.NewV1NetworkSecurityGroupsPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithBody(body) |
| 54 | + postok, postcreated, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsPost(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 55 | + if err != nil { |
| 56 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to create a network security group %s", err)) |
| 57 | + } |
| 58 | + if postok != nil && postok.Payload != nil { |
| 59 | + return postok.Payload, nil |
| 60 | + } |
| 61 | + if postcreated != nil && postcreated.Payload != nil { |
| 62 | + return postcreated.Payload, nil |
| 63 | + } |
| 64 | + return nil, fmt.Errorf("failed to create a network security group") |
| 65 | +} |
| 66 | + |
| 67 | +// Update a network security group |
| 68 | +func (f *IBMPINetworkSecurityGroupClient) Update(id string, body *models.NetworkSecurityGroupUpdate) (*models.NetworkSecurityGroup, error) { |
| 69 | + params := network_security_groups.NewV1NetworkSecurityGroupsIDPutParams().WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).WithNetworkSecurityGroupID(id).WithBody(body) |
| 70 | + resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsIDPut(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 71 | + if err != nil { |
| 72 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to update network security group %s: %w", id, err)) |
| 73 | + } |
| 74 | + if resp == nil || resp.Payload == nil { |
| 75 | + return nil, fmt.Errorf("failed to update network security group %s", id) |
| 76 | + } |
| 77 | + return resp.Payload, nil |
| 78 | +} |
| 79 | + |
| 80 | +// Delete a network security group |
| 81 | +func (f *IBMPINetworkSecurityGroupClient) Delete(id string) error { |
| 82 | + params := network_security_groups.NewV1NetworkSecurityGroupsIDDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkSecurityGroupID(id) |
| 83 | + _, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsIDDelete(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("failed to delete network security group %s: %w", id, err) |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// Add a member to a network security group |
| 91 | +func (f *IBMPINetworkSecurityGroupClient) AddMember(id string, body *models.NetworkSecurityGroupAddMember) (*models.NetworkSecurityGroupMember, error) { |
| 92 | + params := network_security_groups.NewV1NetworkSecurityGroupsMembersPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithNetworkSecurityGroupID(id).WithBody(body) |
| 93 | + resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsMembersPost(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 94 | + if err != nil { |
| 95 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to add member to network security group %s: %w", id, err)) |
| 96 | + } |
| 97 | + if resp == nil || resp.Payload == nil { |
| 98 | + return nil, fmt.Errorf("failed to add member to network security group %s", id) |
| 99 | + } |
| 100 | + return resp.Payload, nil |
| 101 | +} |
| 102 | + |
| 103 | +// Deleta a member from a network securti group |
| 104 | +func (f *IBMPINetworkSecurityGroupClient) DeleteMember(id, memberId string) error { |
| 105 | + params := network_security_groups.NewV1NetworkSecurityGroupsMembersDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkSecurityGroupID(id).WithNetworkSecurityGroupMemberID(memberId) |
| 106 | + _, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsMembersDelete(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 107 | + if err != nil { |
| 108 | + return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete member %s from network security group %s: %w", memberId, id, err)) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// Add a rule to a network security group |
| 114 | +func (f *IBMPINetworkSecurityGroupClient) AddRule(id string, body *models.NetworkSecurityGroupAddRule) (*models.NetworkSecurityGroupRule, error) { |
| 115 | + params := network_security_groups.NewV1NetworkSecurityGroupsRulesPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithNetworkSecurityGroupID(id).WithBody(body) |
| 116 | + resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsRulesPost(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 117 | + if err != nil { |
| 118 | + return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to add rule to network security group %s: %w", id, err)) |
| 119 | + } |
| 120 | + if resp == nil || resp.Payload == nil { |
| 121 | + return nil, fmt.Errorf("failed to add rule to network security group %s", id) |
| 122 | + } |
| 123 | + return resp.Payload, nil |
| 124 | +} |
| 125 | + |
| 126 | +// Delete a rule from a network security group |
| 127 | +func (f *IBMPINetworkSecurityGroupClient) DeleteRule(id, ruleId string) error { |
| 128 | + params := network_security_groups.NewV1NetworkSecurityGroupsRulesDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkSecurityGroupID(id).WithNetworkSecurityGroupRuleID(ruleId) |
| 129 | + _, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsRulesDelete(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 130 | + if err != nil { |
| 131 | + return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete rule %s from network security group %s: %w", ruleId, id, err)) |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +// Action on a network security group |
| 137 | +func (f *IBMPINetworkSecurityGroupClient) Action(body *models.NetworkSecurityGroupsAction) error { |
| 138 | + params := network_security_groups.NewV1NetworkSecurityGroupsActionPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithBody(body) |
| 139 | + _, _, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsActionPost(params, f.session.AuthInfo(f.cloudInstanceID)) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("failed to perform action :%w", err) |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
0 commit comments