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

Method CreatePolicy

backend/store/policy.go:549–574  ·  view source on GitHub ↗

CreatePolicy creates a policy.

(ctx context.Context, create *PolicyMessage)

Source from the content-addressed store, hash-verified

547
548// CreatePolicy creates a policy.
549func (s *Store) CreatePolicy(ctx context.Context, create *PolicyMessage) (*PolicyMessage, error) {
550 if create.Workspace == "" {
551 return nil, errors.Errorf("workspace is required to create policy (resource_type=%s, resource=%s, type=%s)", create.ResourceType, create.Resource, create.Type)
552 }
553 tx, err := s.GetDB().BeginTx(ctx, nil)
554 if err != nil {
555 return nil, err
556 }
557 defer tx.Rollback()
558
559 policy, err := upsertPolicyImpl(ctx, tx, create)
560 if err != nil {
561 return nil, err
562 }
563
564 if err := tx.Commit(); err != nil {
565 return nil, err
566 }
567
568 s.policyCache.Add(getPolicyCacheKey(policy.Workspace, policy.ResourceType, policy.Resource, policy.Type), policy)
569 if policy.Type == storepb.Policy_IAM {
570 s.iamPolicyCache.Remove(getIamPolicyCacheKey(policy.Workspace, policy.ResourceType, policy.Resource))
571 }
572
573 return policy, nil
574}
575
576// UpdatePolicy updates the policy.
577func (s *Store) UpdatePolicy(ctx context.Context, patch *UpdatePolicyMessage) (*PolicyMessage, error) {

Callers 1

Calls 5

GetDBMethod · 0.95
upsertPolicyImplFunction · 0.85
getPolicyCacheKeyFunction · 0.85
getIamPolicyCacheKeyFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected