Skip to content

Commit e92444d

Browse files
michaelkadpowervs-ibmAlexander-Kita
authored
Network Security Group (#489)
* Generated Swagger client from service-broker commit 01eebb83bef64e16e87162c9c7654c8a166e5086 (#431) * Add Network Address Group (#435) * Network Security Group (#434) * Network Security Group * Add nsg action * Fix typo in file name (#442) * Update NSG[rm direction], NI[ch PvmInstance ->Instance, rm NetworkSecurityGroupID](#445) * Update [M]NAg,NSG: member, rules; SysPool: CoreIncrement SB commit c78e75aa6d199faa5196e1bac0488071a074b8c9 (#451) * Update NSG and NAG types (#452) * Change NSG and NAG response types * Fix deletes * [M] NetworkSecurityGroupRuleProtocol: IcmpTypes float64-> int64 SB commit 8052ef5f496669f885519678a3e9fe6fe638cf5d (#454) * [M] NetworkSecurityGroupRulePort: min and max limit, NSG Rule: rm Direction and Name SB commit 4a6fb79d2076748aedcfcfa65b727fc413e8a7df (#455) * Update [M] NAG, NSG: UserTags add omitempty SB commit 6b88f9d59f3ce12ee6b320cfa7b7727243bc661a (#456) Update [M] NAG, NSG: UserTags add omitempty * Fix NAG Update (#457) * Fix NAG Member Create (#458) --------- Co-authored-by: powervs-ibm <[email protected]> Co-authored-by: Alexander-Kita <[email protected]>
1 parent 464872d commit e92444d

File tree

61 files changed

+16342
-86
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+16342
-86
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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_address_groups"
10+
"github.com/IBM-Cloud/power-go-client/power/models"
11+
)
12+
13+
// IBMPINetworkAddressGroupClient
14+
type IBMPINetworkAddressGroupClient struct {
15+
IBMPIClient
16+
}
17+
18+
// NewIBMPINetworkAddressGroupClient
19+
func NewIBMPINetworkAddressGroupClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPINetworkAddressGroupClient {
20+
return &IBMPINetworkAddressGroupClient{
21+
*NewIBMPIClient(ctx, sess, cloudInstanceID),
22+
}
23+
}
24+
25+
// Create a new Network Address Group
26+
func (f *IBMPINetworkAddressGroupClient) Create(body *models.NetworkAddressGroupCreate) (*models.NetworkAddressGroup, error) {
27+
params := network_address_groups.NewV1NetworkAddressGroupsPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithBody(body)
28+
postok, postcreated, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsPost(params, f.session.AuthInfo(f.cloudInstanceID))
29+
if err != nil {
30+
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to create a network address group %s", err))
31+
}
32+
if postok != nil && postok.Payload != nil {
33+
return postok.Payload, nil
34+
}
35+
if postcreated != nil && postcreated.Payload != nil {
36+
return postcreated.Payload, nil
37+
}
38+
return nil, fmt.Errorf("failed to create a network address group")
39+
}
40+
41+
// Get the list of Network Address Groups for a workspace
42+
func (f *IBMPINetworkAddressGroupClient) GetAll() (*models.NetworkAddressGroups, error) {
43+
params := network_address_groups.NewV1NetworkAddressGroupsGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut)
44+
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsGet(params, f.session.AuthInfo(f.cloudInstanceID))
45+
if err != nil {
46+
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network address groups %s", err))
47+
}
48+
if resp == nil || resp.Payload == nil {
49+
return nil, fmt.Errorf("failed to get network address groups")
50+
}
51+
return resp.Payload, nil
52+
53+
}
54+
55+
// Get the detail of a Network Address Group
56+
func (f *IBMPINetworkAddressGroupClient) Get(id string) (*models.NetworkAddressGroup, error) {
57+
params := network_address_groups.NewV1NetworkAddressGroupsIDGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).WithNetworkAddressGroupID(id)
58+
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsIDGet(params, f.session.AuthInfo(f.cloudInstanceID))
59+
if err != nil {
60+
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network address group %s: %w", id, err))
61+
}
62+
if resp == nil || resp.Payload == nil {
63+
return nil, fmt.Errorf("failed to get network address group %s", id)
64+
}
65+
return resp.Payload, nil
66+
67+
}
68+
69+
// Update a Network Address Group
70+
func (f *IBMPINetworkAddressGroupClient) Update(id string, body *models.NetworkAddressGroupUpdate) (*models.NetworkAddressGroup, error) {
71+
params := network_address_groups.NewV1NetworkAddressGroupsIDPutParams().WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).WithNetworkAddressGroupID(id).WithBody(body)
72+
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsIDPut(params, f.session.AuthInfo(f.cloudInstanceID))
73+
if err != nil {
74+
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to update network address group %s: %w", id, err))
75+
}
76+
if resp == nil || resp.Payload == nil {
77+
return nil, fmt.Errorf("failed to update network address group %s", id)
78+
}
79+
return resp.Payload, nil
80+
}
81+
82+
// Delete a Network Address Group from a workspace
83+
func (f *IBMPINetworkAddressGroupClient) Delete(id string) error {
84+
params := network_address_groups.NewV1NetworkAddressGroupsIDDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkAddressGroupID(id)
85+
_, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsIDDelete(params, f.session.AuthInfo(f.cloudInstanceID))
86+
if err != nil {
87+
return fmt.Errorf("failed to delete network address group %s: %w", id, err)
88+
}
89+
return nil
90+
}
91+
92+
// Add a member to a Network Address Group
93+
func (f *IBMPINetworkAddressGroupClient) AddMember(id string, body *models.NetworkAddressGroupAddMember) (*models.NetworkAddressGroupMember, error) {
94+
params := network_address_groups.NewV1NetworkAddressGroupsMembersPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithNetworkAddressGroupID(id).WithBody(body)
95+
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsMembersPost(params, f.session.AuthInfo(f.cloudInstanceID))
96+
if err != nil {
97+
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to add member to network address group %s: %w", id, err))
98+
}
99+
if resp == nil || resp.Payload == nil {
100+
return nil, fmt.Errorf("failed to add member to network address group %s", id)
101+
}
102+
return resp.Payload, nil
103+
}
104+
105+
// Delete the member from a Network Address Group
106+
func (f *IBMPINetworkAddressGroupClient) DeleteMember(id, memberId string) error {
107+
params := network_address_groups.NewV1NetworkAddressGroupsMembersDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkAddressGroupID(id).WithNetworkAddressGroupMemberID(memberId)
108+
_, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsMembersDelete(params, f.session.AuthInfo(f.cloudInstanceID))
109+
if err != nil {
110+
return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete member %s from network address group %s: %w", memberId, id, err))
111+
}
112+
113+
return nil
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)