GetPolicy gets a policy.
(ctx context.Context, find *FindPolicyMessage)
| 437 | |
| 438 | // GetPolicy gets a policy. |
| 439 | func (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. |
| 470 | func (s *Store) ListPolicies(ctx context.Context, find *FindPolicyMessage) ([]*PolicyMessage, error) { |
no test coverage detected