Skip to content

Commit

Permalink
Network Security Group (IBM-Cloud#489)
Browse files Browse the repository at this point in the history
* Generated Swagger client from service-broker commit 01eebb83bef64e16e87162c9c7654c8a166e5086 (IBM-Cloud#431)

* Add Network Address Group (IBM-Cloud#435)

* Network Security Group (IBM-Cloud#434)

* Network Security Group

* Add nsg action

* Fix typo in file name (IBM-Cloud#442)

* Update NSG[rm direction], NI[ch PvmInstance ->Instance, rm NetworkSecurityGroupID](IBM-Cloud#445)

* Update [M]NAg,NSG: member, rules; SysPool: CoreIncrement SB commit c78e75aa6d199faa5196e1bac0488071a074b8c9 (IBM-Cloud#451)

* Update NSG and NAG types (IBM-Cloud#452)

* Change NSG and NAG response types

* Fix deletes

* [M] NetworkSecurityGroupRuleProtocol: IcmpTypes float64-> int64 SB commit 8052ef5f496669f885519678a3e9fe6fe638cf5d (IBM-Cloud#454)

* [M] NetworkSecurityGroupRulePort: min and max limit, NSG Rule: rm Direction and Name  SB commit 4a6fb79d2076748aedcfcfa65b727fc413e8a7df (IBM-Cloud#455)

* Update [M] NAG, NSG: UserTags add omitempty SB commit 6b88f9d59f3ce12ee6b320cfa7b7727243bc661a (IBM-Cloud#456)

Update [M] NAG, NSG: UserTags add omitempty

* Fix NAG Update (IBM-Cloud#457)

* Fix NAG Member Create (IBM-Cloud#458)

---------

Co-authored-by: powervs-ibm <[email protected]>
Co-authored-by: Alexander-Kita <[email protected]>
  • Loading branch information
3 people authored Oct 14, 2024
1 parent 464872d commit e92444d
Show file tree
Hide file tree
Showing 61 changed files with 16,342 additions and 86 deletions.
114 changes: 114 additions & 0 deletions clients/instance/ibm-pi-network-address-group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package instance

import (
"context"
"fmt"

"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/network_address_groups"
"github.com/IBM-Cloud/power-go-client/power/models"
)

// IBMPINetworkAddressGroupClient
type IBMPINetworkAddressGroupClient struct {
IBMPIClient
}

// NewIBMPINetworkAddressGroupClient
func NewIBMPINetworkAddressGroupClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPINetworkAddressGroupClient {
return &IBMPINetworkAddressGroupClient{
*NewIBMPIClient(ctx, sess, cloudInstanceID),
}
}

// Create a new Network Address Group
func (f *IBMPINetworkAddressGroupClient) Create(body *models.NetworkAddressGroupCreate) (*models.NetworkAddressGroup, error) {
params := network_address_groups.NewV1NetworkAddressGroupsPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithBody(body)
postok, postcreated, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to create a network address group %s", err))
}
if postok != nil && postok.Payload != nil {
return postok.Payload, nil
}
if postcreated != nil && postcreated.Payload != nil {
return postcreated.Payload, nil
}
return nil, fmt.Errorf("failed to create a network address group")
}

// Get the list of Network Address Groups for a workspace
func (f *IBMPINetworkAddressGroupClient) GetAll() (*models.NetworkAddressGroups, error) {
params := network_address_groups.NewV1NetworkAddressGroupsGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut)
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network address groups %s", err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to get network address groups")
}
return resp.Payload, nil

}

// Get the detail of a Network Address Group
func (f *IBMPINetworkAddressGroupClient) Get(id string) (*models.NetworkAddressGroup, error) {
params := network_address_groups.NewV1NetworkAddressGroupsIDGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).WithNetworkAddressGroupID(id)
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsIDGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network address group %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to get network address group %s", id)
}
return resp.Payload, nil

}

// Update a Network Address Group
func (f *IBMPINetworkAddressGroupClient) Update(id string, body *models.NetworkAddressGroupUpdate) (*models.NetworkAddressGroup, error) {
params := network_address_groups.NewV1NetworkAddressGroupsIDPutParams().WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).WithNetworkAddressGroupID(id).WithBody(body)
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsIDPut(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to update network address group %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to update network address group %s", id)
}
return resp.Payload, nil
}

// Delete a Network Address Group from a workspace
func (f *IBMPINetworkAddressGroupClient) Delete(id string) error {
params := network_address_groups.NewV1NetworkAddressGroupsIDDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkAddressGroupID(id)
_, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsIDDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to delete network address group %s: %w", id, err)
}
return nil
}

// Add a member to a Network Address Group
func (f *IBMPINetworkAddressGroupClient) AddMember(id string, body *models.NetworkAddressGroupAddMember) (*models.NetworkAddressGroupMember, error) {
params := network_address_groups.NewV1NetworkAddressGroupsMembersPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithNetworkAddressGroupID(id).WithBody(body)
resp, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsMembersPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to add member to network address group %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to add member to network address group %s", id)
}
return resp.Payload, nil
}

// Delete the member from a Network Address Group
func (f *IBMPINetworkAddressGroupClient) DeleteMember(id, memberId string) error {
params := network_address_groups.NewV1NetworkAddressGroupsMembersDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkAddressGroupID(id).WithNetworkAddressGroupMemberID(memberId)
_, err := f.session.Power.NetworkAddressGroups.V1NetworkAddressGroupsMembersDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete member %s from network address group %s: %w", memberId, id, err))
}

return nil
}
144 changes: 144 additions & 0 deletions clients/instance/ibm-pi-network-security-group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package instance

import (
"context"
"fmt"

"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/network_security_groups"
"github.com/IBM-Cloud/power-go-client/power/models"
)

// IBMPINetworkSecurityGroupClient
type IBMPINetworkSecurityGroupClient struct {
IBMPIClient
}

// NewIBMIPINetworkSecurityGroupClient
func NewIBMIPINetworkSecurityGroupClient(ctx context.Context, sess *ibmpisession.IBMPISession, cloudInstanceID string) *IBMPINetworkSecurityGroupClient {
return &IBMPINetworkSecurityGroupClient{
*NewIBMPIClient(ctx, sess, cloudInstanceID),
}
}

// Get a network security group
func (f *IBMPINetworkSecurityGroupClient) Get(id string) (*models.NetworkSecurityGroup, error) {
params := network_security_groups.NewV1NetworkSecurityGroupsIDGetParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut).WithNetworkSecurityGroupID(id)
resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsIDGet(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network security group %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to get network security group %s", id)
}
return resp.Payload, nil
}

// Get all network security groups
func (f *IBMPINetworkSecurityGroupClient) GetAll() (*models.NetworkSecurityGroups, error) {
params := network_security_groups.NewV1NetworkSecurityGroupsListParams().WithContext(f.ctx).WithTimeout(helpers.PIGetTimeOut)
resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsList(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to get network security groups %s", err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to get network security groups")
}
return resp.Payload, nil
}

// Create a network security group
func (f *IBMPINetworkSecurityGroupClient) Create(body *models.NetworkSecurityGroupCreate) (*models.NetworkSecurityGroup, error) {
params := network_security_groups.NewV1NetworkSecurityGroupsPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithBody(body)
postok, postcreated, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to create a network security group %s", err))
}
if postok != nil && postok.Payload != nil {
return postok.Payload, nil
}
if postcreated != nil && postcreated.Payload != nil {
return postcreated.Payload, nil
}
return nil, fmt.Errorf("failed to create a network security group")
}

// Update a network security group
func (f *IBMPINetworkSecurityGroupClient) Update(id string, body *models.NetworkSecurityGroupUpdate) (*models.NetworkSecurityGroup, error) {
params := network_security_groups.NewV1NetworkSecurityGroupsIDPutParams().WithContext(f.ctx).WithTimeout(helpers.PIUpdateTimeOut).WithNetworkSecurityGroupID(id).WithBody(body)
resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsIDPut(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to update network security group %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to update network security group %s", id)
}
return resp.Payload, nil
}

// Delete a network security group
func (f *IBMPINetworkSecurityGroupClient) Delete(id string) error {
params := network_security_groups.NewV1NetworkSecurityGroupsIDDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkSecurityGroupID(id)
_, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsIDDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to delete network security group %s: %w", id, err)
}
return nil
}

// Add a member to a network security group
func (f *IBMPINetworkSecurityGroupClient) AddMember(id string, body *models.NetworkSecurityGroupAddMember) (*models.NetworkSecurityGroupMember, error) {
params := network_security_groups.NewV1NetworkSecurityGroupsMembersPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithNetworkSecurityGroupID(id).WithBody(body)
resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsMembersPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to add member to network security group %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to add member to network security group %s", id)
}
return resp.Payload, nil
}

// Deleta a member from a network securti group
func (f *IBMPINetworkSecurityGroupClient) DeleteMember(id, memberId string) error {
params := network_security_groups.NewV1NetworkSecurityGroupsMembersDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkSecurityGroupID(id).WithNetworkSecurityGroupMemberID(memberId)
_, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsMembersDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete member %s from network security group %s: %w", memberId, id, err))
}
return nil
}

// Add a rule to a network security group
func (f *IBMPINetworkSecurityGroupClient) AddRule(id string, body *models.NetworkSecurityGroupAddRule) (*models.NetworkSecurityGroupRule, error) {
params := network_security_groups.NewV1NetworkSecurityGroupsRulesPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithNetworkSecurityGroupID(id).WithBody(body)
resp, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsRulesPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return nil, ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to add rule to network security group %s: %w", id, err))
}
if resp == nil || resp.Payload == nil {
return nil, fmt.Errorf("failed to add rule to network security group %s", id)
}
return resp.Payload, nil
}

// Delete a rule from a network security group
func (f *IBMPINetworkSecurityGroupClient) DeleteRule(id, ruleId string) error {
params := network_security_groups.NewV1NetworkSecurityGroupsRulesDeleteParams().WithContext(f.ctx).WithTimeout(helpers.PIDeleteTimeOut).WithNetworkSecurityGroupID(id).WithNetworkSecurityGroupRuleID(ruleId)
_, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsRulesDelete(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return ibmpisession.SDKFailWithAPIError(err, fmt.Errorf("failed to delete rule %s from network security group %s: %w", ruleId, id, err))
}
return nil
}

// Action on a network security group
func (f *IBMPINetworkSecurityGroupClient) Action(body *models.NetworkSecurityGroupsAction) error {
params := network_security_groups.NewV1NetworkSecurityGroupsActionPostParams().WithContext(f.ctx).WithTimeout(helpers.PICreateTimeOut).WithBody(body)
_, _, err := f.session.Power.NetworkSecurityGroups.V1NetworkSecurityGroupsActionPost(params, f.session.AuthInfo(f.cloudInstanceID))
if err != nil {
return fmt.Errorf("failed to perform action :%w", err)
}
return nil
}
Loading

0 comments on commit e92444d

Please sign in to comment.