guardian icon indicating copy to clipboard operation
guardian copied to clipboard

Support dry-run option for create & update in policy & provider

Open rahmatrhd opened this issue 3 years ago • 0 comments

Describe the solution you'd like

  1. accept dry_run flag in payload
  message CreatePolicyRequest {
      Policy policy = 1;
+     bool dry_run = 2;
  }

  message UpdatePolicyRequest {
      string id = 1;
      Policy policy = 2;
+     bool dry_run = 3;
  }

  message CreateProviderRequest {
      ProviderConfig config = 1;
+     bool dry_run = 2;
  }

  message UpdateProviderRequest {
      string id = 1;
      ProviderConfig config = 2;
+     bool dry_run = 2;
  }
  1. add support to policy service and provider service to accept dry_run info from context
// core/policy/service.go

func WithDryRun(ctx context.Context) context.Context {
  return context.WithValue(ctx, "dry_run")
}

func isDryRun(ctx) bool {
  return ctx.Value("dry_run").(bool)
}

...

func (s *Service) Create(ctx context.Context, p *domain.Policy) error {
  ...

  dryRun := isDryRun(ctx)
  if !dryRun {
    if err := s.policyRepository.Create(p); err != nil {
		return err
	}
  }

  return nil
}

  1. pass dry_run flag to service in grpc handler
// api/handler/v1beta1/grpc.go GRPCServer.CreatePolicy
...

if req.GetDryRun() {
  ctx = policy.WithDryRun(ctx)
}

if err := s.policyService.Create(ctx, policy); err != nil {
  return nil, status.Errorf(codes.Internal, "failed to create policy: %v", err)
}

...

rahmatrhd avatar Jan 25 '22 03:01 rahmatrhd