|
1 | 1 | package adapters
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "cloud.google.com/go/compute/apiv1/computepb" |
| 9 | + "google.golang.org/api/iterator" |
| 10 | + |
| 11 | + "github.com/overmindtech/cli/sdp-go" |
| 12 | + "github.com/overmindtech/cli/sources" |
4 | 13 | gcpshared "github.com/overmindtech/cli/sources/gcp/shared"
|
5 | 14 | "github.com/overmindtech/cli/sources/shared"
|
6 | 15 | )
|
7 | 16 |
|
8 |
| -var ComputeSecurityPolicy = shared.NewItemType(gcpshared.GCP, gcpshared.Compute, gcpshared.SecurityPolicy) |
| 17 | +var ( |
| 18 | + ComputeSecurityPolicy = shared.NewItemType(gcpshared.GCP, gcpshared.Compute, gcpshared.SecurityPolicy) |
| 19 | + ComputeRule = shared.NewItemType(gcpshared.GCP, gcpshared.Compute, gcpshared.Rule) |
| 20 | + |
| 21 | + ComputeSecurityPolicyLookupByName = shared.NewItemTypeLookup("name", ComputeSecurityPolicy) |
| 22 | +) |
| 23 | + |
| 24 | +type computeSecurityPolicyWrapper struct { |
| 25 | + client gcpshared.ComputeSecurityPolicyClient |
| 26 | + |
| 27 | + *gcpshared.ProjectBase |
| 28 | +} |
| 29 | + |
| 30 | +// NewComputeSecurityPolicy creates a new computeSecurityPolicyWrapper instance |
| 31 | +func NewComputeSecurityPolicy(client gcpshared.ComputeSecurityPolicyClient, projectID string) sources.ListableWrapper { |
| 32 | + return &computeSecurityPolicyWrapper{ |
| 33 | + client: client, |
| 34 | + ProjectBase: gcpshared.NewProjectBase( |
| 35 | + projectID, |
| 36 | + sdp.AdapterCategory_ADAPTER_CATEGORY_SECURITY, |
| 37 | + ComputeSecurityPolicy, |
| 38 | + ), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// PotentialLinks returns the potential links for the compute forwarding rule wrapper |
| 43 | +func (c computeSecurityPolicyWrapper) PotentialLinks() map[shared.ItemType]bool { |
| 44 | + return shared.NewItemTypesSet( |
| 45 | + ComputeRule, |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +// TerraformMappings returns the Terraform mappings for the compute security policy wrapper |
| 50 | +func (c computeSecurityPolicyWrapper) TerraformMappings() []*sdp.TerraformMapping { |
| 51 | + return []*sdp.TerraformMapping{ |
| 52 | + { |
| 53 | + TerraformMethod: sdp.QueryMethod_GET, |
| 54 | + TerraformQueryMap: "google_compute_security_policy.name", |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// GetLookups returns the lookups for the compute security policy wrapper |
| 60 | +func (c computeSecurityPolicyWrapper) GetLookups() sources.ItemTypeLookups { |
| 61 | + return sources.ItemTypeLookups{ |
| 62 | + ComputeSecurityPolicyLookupByName, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Get retrieves a compute security policy by its name |
| 67 | +func (c computeSecurityPolicyWrapper) Get(ctx context.Context, queryParts ...string) (*sdp.Item, *sdp.QueryError) { |
| 68 | + req := &computepb.GetSecurityPolicyRequest{ |
| 69 | + Project: c.ProjectID(), |
| 70 | + SecurityPolicy: queryParts[0], |
| 71 | + } |
| 72 | + |
| 73 | + securityPolicy, err := c.client.Get(ctx, req) |
| 74 | + if err != nil { |
| 75 | + return nil, gcpshared.QueryError(err) |
| 76 | + } |
| 77 | + |
| 78 | + item, sdpErr := c.gcpComputeSecurityPolicyToSDPItem(securityPolicy) |
| 79 | + if sdpErr != nil { |
| 80 | + return nil, sdpErr |
| 81 | + } |
| 82 | + |
| 83 | + return item, nil |
| 84 | +} |
| 85 | + |
| 86 | +// List lists compute security policies and converts them to sdp.Items. |
| 87 | +func (c computeSecurityPolicyWrapper) List(ctx context.Context) ([]*sdp.Item, *sdp.QueryError) { |
| 88 | + it := c.client.List(ctx, &computepb.ListSecurityPoliciesRequest{ |
| 89 | + Project: c.ProjectID(), |
| 90 | + }) |
| 91 | + |
| 92 | + var items []*sdp.Item |
| 93 | + for { |
| 94 | + securityPolicy, err := it.Next() |
| 95 | + if errors.Is(err, iterator.Done) { |
| 96 | + break |
| 97 | + } |
| 98 | + if err != nil { |
| 99 | + return nil, gcpshared.QueryError(err) |
| 100 | + } |
| 101 | + |
| 102 | + item, sdpErr := c.gcpComputeSecurityPolicyToSDPItem(securityPolicy) |
| 103 | + if sdpErr != nil { |
| 104 | + return nil, sdpErr |
| 105 | + } |
| 106 | + |
| 107 | + items = append(items, item) |
| 108 | + } |
| 109 | + |
| 110 | + return items, nil |
| 111 | +} |
| 112 | + |
| 113 | +// gcpComputeSecurityPolicyToSDPItem converts a GCP Security Policy to an SDP Item |
| 114 | +func (c computeSecurityPolicyWrapper) gcpComputeSecurityPolicyToSDPItem(securityPolicy *computepb.SecurityPolicy) (*sdp.Item, *sdp.QueryError) { |
| 115 | + attributes, err := shared.ToAttributesWithExclude(securityPolicy, "labels") |
| 116 | + if err != nil { |
| 117 | + return nil, &sdp.QueryError{ |
| 118 | + ErrorType: sdp.QueryError_OTHER, |
| 119 | + ErrorString: err.Error(), |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + sdpItem := &sdp.Item{ |
| 124 | + Type: ComputeSecurityPolicy.String(), |
| 125 | + UniqueAttribute: "name", |
| 126 | + Attributes: attributes, |
| 127 | + Scope: c.DefaultScope(), |
| 128 | + Tags: securityPolicy.GetLabels(), |
| 129 | + } |
| 130 | + |
| 131 | + // Link to associated rules |
| 132 | + // API Reference: https://cloud.google.com/compute/docs/reference/rest/v1/securityPolicies/getRule |
| 133 | + // Rules can be inserted into a security policy using the following API call: |
| 134 | + // GET https://compute.googleapis.com/compute/v1/projects/{project}/global/securityPolicies/{securityPolicy}/getRule |
| 135 | + for _, rule := range securityPolicy.GetRules() { |
| 136 | + policyName := securityPolicy.GetName() |
| 137 | + rulePriority := strconv.Itoa(int(rule.GetPriority())) |
| 138 | + sdpItem.LinkedItemQueries = append(sdpItem.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 139 | + Query: &sdp.Query{ |
| 140 | + Type: ComputeRule.String(), |
| 141 | + Method: sdp.QueryMethod_GET, |
| 142 | + Query: shared.CompositeLookupKey(policyName, rulePriority), |
| 143 | + Scope: c.ProjectID(), |
| 144 | + }, |
| 145 | + BlastPropagation: &sdp.BlastPropagation{In: false, Out: true}, |
| 146 | + }) |
| 147 | + |
| 148 | + } |
| 149 | + return sdpItem, nil |
| 150 | +} |
0 commit comments