forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mockGCP for compute firewall policy rule
- Loading branch information
Showing
3 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,6 +226,132 @@ func (s *FirewallPoliciesV1) Delete(ctx context.Context, req *pb.DeleteFirewallP | |
}) | ||
} | ||
|
||
func (s *FirewallPoliciesV1) GetRule(ctx context.Context, req *pb.GetRuleFirewallPolicyRequest) (*pb.FirewallPolicyRule, error) { | ||
reqName := "locations/global/firewallPolicies/" + req.GetFirewallPolicy() | ||
name, err := s.parseFirewallPolicyName(reqName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fqn := name.String() | ||
|
||
obj := &pb.FirewallPolicy{} | ||
if err := s.storage.Get(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
var rule *pb.FirewallPolicyRule | ||
rules := obj.GetRules() | ||
if len(rules) == 0 { | ||
return nil, err | ||
} | ||
|
||
for _, r := range rules { | ||
if r.Priority == req.Priority { | ||
rule = r | ||
} | ||
} | ||
if rule == nil { | ||
return nil, status.Errorf(codes.NotFound, "Invalid value for field 'priority': '%q'. The firewall policy does not contain a rule at priority %q.", strconv.Itoa(int(*req.Priority)), strconv.Itoa(int(*req.Priority))) | ||
} | ||
|
||
return rule, nil | ||
} | ||
|
||
func (s *FirewallPoliciesV1) AddRule(ctx context.Context, req *pb.AddRuleFirewallPolicyRequest) (*pb.Operation, error) { | ||
reqName := "locations/global/firewallPolicies/" + req.GetFirewallPolicy() | ||
name, err := s.parseFirewallPolicyName(reqName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fqn := name.String() | ||
|
||
obj := &pb.FirewallPolicy{} | ||
if err := s.storage.Get(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
obj.Rules = []*pb.FirewallPolicyRule{req.GetFirewallPolicyRuleResource()} | ||
|
||
if err := s.storage.Update(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
op := &pb.Operation{ | ||
TargetId: obj.Id, | ||
TargetLink: obj.SelfLink, | ||
OperationType: PtrTo("addFirewallRuleToFirewallPolicy"), | ||
User: PtrTo("[email protected]"), | ||
} | ||
return s.startGlobalOrganizationLRO(ctx, op, func() (proto.Message, error) { | ||
return obj, nil | ||
}) | ||
} | ||
|
||
func (s *FirewallPoliciesV1) PatchRule(ctx context.Context, req *pb.PatchRuleFirewallPolicyRequest) (*pb.Operation, error) { | ||
reqName := "locations/global/firewallPolicies/" + req.GetFirewallPolicy() | ||
|
||
name, err := s.parseFirewallPolicyName(reqName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fqn := name.String() | ||
obj := &pb.FirewallPolicy{} | ||
if err := s.storage.Get(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
obj.Rules = []*pb.FirewallPolicyRule{req.GetFirewallPolicyRuleResource()} | ||
|
||
if err := s.storage.Update(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
op := &pb.Operation{ | ||
TargetId: obj.Id, | ||
TargetLink: obj.SelfLink, | ||
OperationType: PtrTo("patchFirewallRuleInFirewallPolicy"), | ||
User: PtrTo("[email protected]"), | ||
// patch operation finished super fast | ||
Progress: PtrTo(int32(100)), | ||
Status: PtrTo(pb.Operation_DONE), | ||
} | ||
return s.startGlobalOrganizationLRO(ctx, op, func() (proto.Message, error) { | ||
return obj, nil | ||
}) | ||
} | ||
func (s *FirewallPoliciesV1) RemoveRule(ctx context.Context, req *pb.RemoveRuleFirewallPolicyRequest) (*pb.Operation, error) { | ||
reqName := "locations/global/firewallPolicies/" + req.GetFirewallPolicy() | ||
name, err := s.parseFirewallPolicyName(reqName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fqn := name.String() | ||
|
||
obj := &pb.FirewallPolicy{} | ||
if err := s.storage.Get(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
obj.Rules = nil | ||
if err := s.storage.Update(ctx, fqn, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
op := &pb.Operation{ | ||
TargetId: obj.Id, | ||
TargetLink: obj.SelfLink, | ||
OperationType: PtrTo("removeFirewallRuleFromFirewallPolicy"), | ||
User: PtrTo("[email protected]"), | ||
} | ||
return s.startGlobalOrganizationLRO(ctx, op, func() (proto.Message, error) { | ||
return obj, nil | ||
}) | ||
} | ||
|
||
type firewallPolicyName struct { | ||
Name string | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters