yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/waf_rule_groups.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aws
    16  
    17  import (
    18  	"github.com/aws/aws-sdk-go/service/wafv2"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  )
    23  
    24  type SWafRuleGroup struct {
    25  	Description string
    26  	Name        string
    27  	VendorName  string
    28  	Capacity    int `json:"Capacity"`
    29  	Rules       []SWafRule
    30  }
    31  
    32  func (self *SRegion) ListAvailableManagedRuleGroups(scope string) ([]SWafRuleGroup, error) {
    33  	if scope == SCOPE_CLOUDFRONT && self.RegionId != "us-east-1" {
    34  		return []SWafRuleGroup{}, nil
    35  	}
    36  	client, err := self.getWafClient()
    37  	if err != nil {
    38  		return nil, errors.Wrapf(err, "getWafClient")
    39  	}
    40  	ret := []SWafRuleGroup{}
    41  	input := wafv2.ListAvailableManagedRuleGroupsInput{}
    42  	input.SetScope(scope)
    43  	for {
    44  		resp, err := client.ListAvailableManagedRuleGroups(&input)
    45  		if err != nil {
    46  			return nil, errors.Wrapf(err, "ListAvailableManagedRuleGroups")
    47  		}
    48  		part := []SWafRuleGroup{}
    49  		jsonutils.Update(&part, resp.ManagedRuleGroups)
    50  		ret = append(ret, part...)
    51  		if resp.NextMarker == nil || len(*resp.NextMarker) == 0 {
    52  			break
    53  		}
    54  		input.SetNextMarker(*resp.NextMarker)
    55  	}
    56  	return ret, nil
    57  }
    58  
    59  func (self *SRegion) DescribeManagedRuleGroup(name, scope, vendorName string) (*SWafRuleGroup, error) {
    60  	client, err := self.getWafClient()
    61  	if err != nil {
    62  		return nil, errors.Wrapf(err, "getWafClient")
    63  	}
    64  	input := wafv2.DescribeManagedRuleGroupInput{}
    65  	input.SetName(name)
    66  	input.SetScope(scope)
    67  	input.SetVendorName(vendorName)
    68  	resp, err := client.DescribeManagedRuleGroup(&input)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	ret := &SWafRuleGroup{
    73  		Name:       name,
    74  		VendorName: vendorName,
    75  	}
    76  	return ret, jsonutils.Update(ret, resp)
    77  }
    78  
    79  func (self *SRegion) ListRuleGroups(scope string) ([]SWafRuleGroup, error) {
    80  	if scope == SCOPE_CLOUDFRONT && self.RegionId != "us-east-1" {
    81  		return []SWafRuleGroup{}, nil
    82  	}
    83  	client, err := self.getWafClient()
    84  	if err != nil {
    85  		return nil, errors.Wrapf(err, "getWafClient")
    86  	}
    87  	ret := []SWafRuleGroup{}
    88  	input := wafv2.ListRuleGroupsInput{}
    89  	input.SetScope(scope)
    90  	for {
    91  		resp, err := client.ListRuleGroups(&input)
    92  		if err != nil {
    93  			return nil, errors.Wrapf(err, "ListRuleGroups")
    94  		}
    95  		part := []SWafRuleGroup{}
    96  		jsonutils.Update(&part, resp.RuleGroups)
    97  		ret = append(ret, part...)
    98  		if resp.NextMarker == nil || len(*resp.NextMarker) == 0 {
    99  			break
   100  		}
   101  		input.SetNextMarker(*resp.NextMarker)
   102  	}
   103  	return ret, nil
   104  }
   105  
   106  func (self *SRegion) GetRuleGroup(id, name, scope string) (*SWafRuleGroup, error) {
   107  	client, err := self.getWafClient()
   108  	if err != nil {
   109  		return nil, errors.Wrapf(err, "getWafClient")
   110  	}
   111  	input := wafv2.GetRuleGroupInput{}
   112  	input.SetId(id)
   113  	input.SetName(name)
   114  	input.SetScope(scope)
   115  	resp, err := client.GetRuleGroup(&input)
   116  	if err != nil {
   117  		return nil, errors.Wrapf(err, "GetRuleGroup")
   118  	}
   119  	ret := &SWafRuleGroup{}
   120  	return ret, jsonutils.Update(ret, resp.RuleGroup)
   121  }
   122  
   123  func (self *SRegion) DeleteRuleGroup(id, name, scope, lockToken string) error {
   124  	client, err := self.getWafClient()
   125  	if err != nil {
   126  		return errors.Wrapf(err, "getWafClient")
   127  	}
   128  	input := wafv2.DeleteRuleGroupInput{}
   129  	input.SetId(id)
   130  	input.SetName(name)
   131  	input.SetScope(scope)
   132  	input.SetLockToken(lockToken)
   133  	_, err = client.DeleteRuleGroup(&input)
   134  	return errors.Wrapf(err, "DeleteRuleGroup")
   135  }