-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support backend context for typeinstance commands (#699)
- Loading branch information
Mateusz Kuziemko
authored
Apr 14, 2022
1 parent
8b42710
commit 37750de
Showing
4 changed files
with
121 additions
and
23 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
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 |
---|---|---|
@@ -1,8 +1,38 @@ | ||
package storagebackend | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"capact.io/capact/internal/cli/client" | ||
gqllocalapi "capact.io/capact/pkg/hub/api/graphql/local" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// TypeInstanceValue defines properties for TypeInstance value for every Storage Backend. | ||
type TypeInstanceValue struct { | ||
URL string `json:"url"` | ||
AcceptValue bool `json:"acceptValue"` | ||
ContextSchema interface{} `json:"contextSchema"` | ||
} | ||
|
||
// NewTypeInstanceValue returns a new TypeInstanceValue instance based on backend used by passed TypeInstance. | ||
func NewTypeInstanceValue(ctx context.Context, cli client.Hub, typeInstance *gqllocalapi.TypeInstance) (*TypeInstanceValue, error) { | ||
var typeInstanceValue *TypeInstanceValue | ||
if typeInstance.Backend == nil || typeInstance.Backend.Abstract { | ||
return nil, nil | ||
} | ||
backendTI, err := cli.FindTypeInstance(ctx, typeInstance.Backend.ID) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "while finding backend TypeInstance") | ||
} | ||
valueBytes, err := json.Marshal(backendTI.LatestResourceVersion.Spec.Value) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "while marshaling storage backend value") | ||
} | ||
err = json.Unmarshal(valueBytes, &typeInstanceValue) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "while unmarshaling storage backend value") | ||
} | ||
return typeInstanceValue, nil | ||
} |