MCPcopy Create free account
hub / github.com/bytebase/bytebase / GetPolicy

Method GetPolicy

backend/store/policy.go:439–467  ·  view source on GitHub ↗

GetPolicy gets a policy.

(ctx context.Context, find *FindPolicyMessage)

Source from the content-addressed store, hash-verified

437
438// GetPolicy gets a policy.
439func (s *Store) GetPolicy(ctx context.Context, find *FindPolicyMessage) (*PolicyMessage, error) {
440 if find.ResourceType != nil && find.Resource != nil && find.Type != nil {
441 if v, ok := s.policyCache.Get(getPolicyCacheKey(find.Workspace, *find.ResourceType, *find.Resource, *find.Type)); ok && s.enableCache {
442 return v, nil
443 }
444 }
445
446 // We will always return the resource regardless of its deleted state.
447 find.ShowAll = true
448 policies, err := s.ListPolicies(ctx, find)
449 if err != nil {
450 return nil, err
451 }
452 if len(policies) == 0 {
453 // Cache the policy for not found as well to reduce the look up latency.
454 if find.ResourceType != nil && find.Resource != nil && find.Type != nil {
455 s.policyCache.Add(getPolicyCacheKey(find.Workspace, *find.ResourceType, *find.Resource, *find.Type), nil)
456 }
457 return nil, nil
458 }
459 if len(policies) > 1 {
460 return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d policies with filter %+v, expect 1", len(policies), find)}
461 }
462 policy := policies[0]
463
464 s.policyCache.Add(getPolicyCacheKey(policy.Workspace, policy.ResourceType, policy.Resource, policy.Type), policy)
465
466 return policy, nil
467}
468
469// ListPolicies lists all policies.
470func (s *Store) ListPolicies(ctx context.Context, find *FindPolicyMessage) ([]*PolicyMessage, error) {

Callers 6

getIamPolicyMethod · 0.95
GetRolloutPolicyMethod · 0.95
getQueryDataPolicyMethod · 0.95
GetMaskingRulePolicyMethod · 0.95

Calls 4

ListPoliciesMethod · 0.95
getPolicyCacheKeyFunction · 0.85
GetMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected