github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/secgroups/doc.go (about)

     1  /*
     2  Package secgroups provides the ability to manage security groups through the
     3  Nova API.
     4  
     5  This API has been deprecated and will be removed from a future release of the
     6  Nova API service.
     7  
     8  For environments that support this extension, this package can be used
     9  regardless of if either Neutron or nova-network is used as the cloud's network
    10  service.
    11  
    12  Example to List Security Groups
    13  
    14  	allPages, err := secroups.List(computeClient).AllPages()
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  
    19  	allSecurityGroups, err := secgroups.ExtractSecurityGroups(allPages)
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  
    24  	for _, sg := range allSecurityGroups {
    25  		fmt.Printf("%+v\n", sg)
    26  	}
    27  
    28  Example to List Security Groups by Server
    29  
    30  	serverID := "aab3ad01-9956-4623-a29b-24afc89a7d36"
    31  
    32  	allPages, err := secroups.ListByServer(computeClient, serverID).AllPages()
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  
    37  	allSecurityGroups, err := secgroups.ExtractSecurityGroups(allPages)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	for _, sg := range allSecurityGroups {
    43  		fmt.Printf("%+v\n", sg)
    44  	}
    45  
    46  Example to Create a Security Group
    47  
    48  	createOpts := secgroups.CreateOpts{
    49  		Name:        "group_name",
    50  		Description: "A Security Group",
    51  	}
    52  
    53  	sg, err := secgroups.Create(computeClient, createOpts).Extract()
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  
    58  Example to Create a Security Group Rule
    59  
    60  	sgID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
    61  
    62  	createOpts := secgroups.CreateRuleOpts{
    63  		ParentGroupID: sgID,
    64  		FromPort:      22,
    65  		ToPort:        22,
    66  		IPProtocol:    "tcp",
    67  		CIDR:          "0.0.0.0/0",
    68  	}
    69  
    70  	rule, err := secgroups.CreateRule(computeClient, createOpts).Extract()
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  
    75  Example to Add a Security Group to a Server
    76  
    77  	serverID := "aab3ad01-9956-4623-a29b-24afc89a7d36"
    78  	sgID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
    79  
    80  	err := secgroups.AddServer(computeClient, serverID, sgID).ExtractErr()
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  
    85  Example to Remove a Security Group from a Server
    86  
    87  	serverID := "aab3ad01-9956-4623-a29b-24afc89a7d36"
    88  	sgID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
    89  
    90  	err := secgroups.RemoveServer(computeClient, serverID, sgID).ExtractErr()
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  
    95  Example to Delete a Security Group
    96  
    97  
    98  	sgID := "37d94f8a-d136-465c-ae46-144f0d8ef141"
    99  	err := secgroups.Delete(computeClient, sgID).ExtractErr()
   100  	if err != nil {
   101  		panic(err)
   102  	}
   103  
   104  Example to Delete a Security Group Rule
   105  
   106  	ruleID := "6221fe3e-383d-46c9-a3a6-845e66c1e8b4"
   107  	err := secgroups.DeleteRule(computeClient, ruleID).ExtractErr()
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  */
   112  package secgroups