-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dry-run implementation for tenant import (#15)
* tenant dry storage * fix typo * remove unused fun --------- Co-authored-by: Piotr Janus <[email protected]>
- Loading branch information
1 parent
921d513
commit 2579b10
Showing
6 changed files
with
194 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package data | ||
|
||
import ( | ||
"github.com/cloudentity/acp-client-go/clients/hub/models" | ||
"github.com/cloudentity/cac/internal/cac/utils" | ||
"github.com/go-openapi/strfmt" | ||
) | ||
|
||
type ServerValidator struct{} | ||
|
||
var _ ValidatorApi = &ServerValidator{} | ||
|
||
func (sv *ServerValidator) Validate(data *models.Rfc7396PatchOperation) error { | ||
var ( | ||
err error | ||
serv *models.TreeServer | ||
) | ||
if serv, err = utils.FromPatchToModel[models.TreeServer](*data); err != nil { | ||
return err | ||
} | ||
|
||
if err = serv.Validate(strfmt.Default); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package data | ||
|
||
import ( | ||
"github.com/cloudentity/acp-client-go/clients/hub/models" | ||
"github.com/cloudentity/cac/internal/cac/utils" | ||
"github.com/go-openapi/strfmt" | ||
) | ||
|
||
type TenantValidator struct{} | ||
|
||
var _ ValidatorApi = &TenantValidator{} | ||
|
||
func (sv *TenantValidator) Validate(data *models.Rfc7396PatchOperation) error { | ||
var ( | ||
err error | ||
tenant *models.TreeTenant | ||
) | ||
if tenant, err = utils.FromPatchToModel[models.TreeTenant](*data); err != nil { | ||
return err | ||
} | ||
|
||
if err = tenant.Validate(strfmt.Default); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package data | ||
|
||
import "github.com/cloudentity/acp-client-go/clients/hub/models" | ||
|
||
type ValidatorApi interface { | ||
Validate(data *models.Rfc7396PatchOperation) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"github.com/cloudentity/acp-client-go/clients/hub/models" | ||
"github.com/cloudentity/cac/internal/cac/api" | ||
"github.com/cloudentity/cac/internal/cac/utils" | ||
"github.com/pkg/errors" | ||
"os" | ||
) | ||
|
||
type DryStorage struct { | ||
DelegatedWriter WriterFunc | ||
} | ||
|
||
func InitDryStorage(out string, constr Constructor) (*DryStorage, error) { | ||
var ( | ||
delegatedWriter WriterFunc | ||
err error | ||
) | ||
|
||
if out == "-" { | ||
delegatedWriter = stdWriter | ||
} else if out != "" { | ||
var ( | ||
file *os.File | ||
info os.FileInfo | ||
) | ||
|
||
if file, err = os.OpenFile(out, os.O_RDONLY, 0644); err != nil && !os.IsNotExist(err) { | ||
return nil, err | ||
} else if err == nil { | ||
// file already exists | ||
defer file.Close() | ||
|
||
if info, err = file.Stat(); err != nil { | ||
return nil, err | ||
} | ||
|
||
if info.IsDir() { | ||
dryStorage := constr(&Configuration{ | ||
DirPath: out, | ||
}) | ||
|
||
delegatedWriter = dryStorage.Write | ||
} | ||
} | ||
|
||
if delegatedWriter == nil { | ||
delegatedWriter = flatFileWriter(out) | ||
} | ||
} else { | ||
return nil, errors.New("out cannot be empty") | ||
} | ||
|
||
return &DryStorage{ | ||
DelegatedWriter: delegatedWriter, | ||
}, nil | ||
} | ||
|
||
type WriterFunc func(ctx context.Context, data models.Rfc7396PatchOperation, opts ...api.SourceOpt) error | ||
|
||
func (d *DryStorage) Write(ctx context.Context, data models.Rfc7396PatchOperation, opts ...api.SourceOpt) error { | ||
return d.DelegatedWriter(ctx, data, opts...) | ||
} | ||
|
||
func (d *DryStorage) Read(ctx context.Context, opts ...api.SourceOpt) (models.Rfc7396PatchOperation, error) { | ||
panic("read operation is not implemented for dry storage") | ||
} | ||
|
||
var stdWriter = func(ctx context.Context, data models.Rfc7396PatchOperation, opts ...api.SourceOpt) error { | ||
var ( | ||
bts []byte | ||
err error | ||
) | ||
if bts, err = utils.ToYaml(data); err != nil { | ||
return err | ||
} | ||
|
||
_, err = os.Stdout.Write(bts) | ||
return err | ||
} | ||
|
||
var flatFileWriter = func(out string) WriterFunc { | ||
return func(ctx context.Context, data models.Rfc7396PatchOperation, opts ...api.SourceOpt) error { | ||
var ( | ||
bts []byte | ||
err error | ||
) | ||
|
||
if bts, err = utils.ToYaml(data); err != nil { | ||
return err | ||
} | ||
|
||
if err = os.WriteFile(out, bts, 0644); err != nil { | ||
return err | ||
} | ||
|
||
if bts, err = utils.ToYaml(data); err != nil { | ||
return err | ||
} | ||
|
||
// file does not exist or is not a directory | ||
if err = os.WriteFile(out, bts, 0644); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var _ Storage = &DryStorage{} |