Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dgraph cloud host type #26

Merged
merged 8 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions dgraph_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 Hypermode, Inc.
*/

package manifest

import (
"crypto/sha256"
"encoding/hex"
"fmt"
)

const (
HostTypeDGraphCloud string = "dgraph-cloud"
)

type DGraphCloudHostInfo struct {
Name string `json:"-"`
Type string `json:"type"`
Endpoint string `json:"endpoint"`
Key string `json:"key"`
}

func (p DGraphCloudHostInfo) HostName() string {
return p.Name
}

func (DGraphCloudHostInfo) HostType() string {
return HostTypeDGraphCloud
}

func (h DGraphCloudHostInfo) GetVariables() []string {
return extractVariables(h.Key)
}

func (h DGraphCloudHostInfo) Hash() string {
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
// Concatenate the attributes into a single string
data := fmt.Sprintf("%v|%v|%v|%v", h.Name, h.Type, h.Endpoint, h.Key)

// Compute the SHA-256 hash
hash := sha256.Sum256([]byte(data))

// Convert the hash to a hexadecimal string
hashStr := hex.EncodeToString(hash[:])

return hashStr
}
35 changes: 32 additions & 3 deletions hypermode.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@
"type": {
"type": "string",
"default": "http",
"enum": ["http", "postgresql"],
"description": "Type for the host, such as 'http', 'postgresql'",
"markdownDescription": "Type for the host, such as 'http', 'postgresql'.\n\nReference: https://docs.hypermode.com/define-hosts"
"enum": ["http", "postgresql", "dgraph-cloud"],
"description": "Type for the host, such as 'http', 'postgresql', 'dgraph-cloud'",
"markdownDescription": "Type for the host, such as 'http', 'postgresql', 'dgraph-cloud'.\n\nReference: https://docs.hypermode.com/define-hosts"
}
},
"allOf": [
Expand Down Expand Up @@ -206,6 +206,35 @@
"required": ["connString"],
"additionalProperties": false
}
},
{
"if": {
"properties": { "type": { "const": "dgraph-cloud" } },
"required": ["type"]
},
"then": {
"properties": {
"type": {
"const": "dgraph-cloud"
},
"endpoint": {
"type": "string",
"format": "uri",
"minLength": 1,
"pattern": "^https?://\\S+$",
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
"description": "Full URL endpoint for connections to DGraph Cloud.",
"markdownDescription": "Full URL endpoint for connections to DGraph.\n\nReference: https://docs.hypermode.com/define-hosts"
},
"key": {
"type": "string",
"minLength": 1,
"description": "API key for DGraph Cloud.",
"markdownDescription": "API key for DGraph Cloud.\n\nReference: https://docs.hypermode.com/define-hosts"
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
}
},
"required": ["endpoint", "key"],
"additionalProperties": false
}
}
]
}
Expand Down
7 changes: 7 additions & 0 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func parseManifestJson(data []byte, manifest *HypermodeManifest) error {
}
h.Name = name
manifest.Hosts[name] = h
case HostTypeDGraphCloud:
var h DGraphCloudHostInfo
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
if err := json.Unmarshal(rawHost, &h); err != nil {
return fmt.Errorf("failed to parse manifest: %w", err)
}
h.Name = name
manifest.Hosts[name] = h
default:
return fmt.Errorf("unknown host type: [%s]", hostType.String())
}
Expand Down
21 changes: 21 additions & 0 deletions test/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func TestReadManifest(t *testing.T) {
Type: "postgresql",
ConnStr: "postgresql://{{POSTGRESQL_USERNAME}}:{{POSTGRESQL_PASSWORD}}@1.2.3.4:5432/data?sslmode=disable",
},
"my-dgraph-cloud": manifest.DGraphCloudHostInfo{
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
Name: "my-dgraph-cloud",
Type: "dgraph-cloud",
Endpoint: "https://frozen-mango.eu-central-1.aws.cloud.dgraph.io/graphql",
Key: "{{DGRAPH_KEY}}",
},
},
Collections: map[string]manifest.CollectionInfo{
"collection1": {
Expand Down Expand Up @@ -233,6 +239,20 @@ func TestHPostgresHostInfo_Hash(t *testing.T) {
}
}

func TestDGraphCloudHostInfo_Hash(t *testing.T) {
host := manifest.DGraphCloudHostInfo{
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
Name: "my-dgraph-cloud",
Endpoint: "https://frozen-mango.eu-central-1.aws.cloud.dgraph.io/graphql",
Key: "{{DGRAPH_KEY}}",
}

expectedHash := "8039a6ea0219f417341f5e41230f596776d0f0381f0aac59052c96fea3eb70ba"
actualHash := host.Hash()
if actualHash != expectedHash {
t.Errorf("Expected hash: %s, but got: %s", expectedHash, actualHash)
}
}

func TestGetHostVariablesFromManifest(t *testing.T) {
// This should match the host variables that are present in valid_hypermode.json
expectedVars := map[string][]string{
Expand All @@ -242,6 +262,7 @@ func TestGetHostVariablesFromManifest(t *testing.T) {
"my-rest-api": {"API_TOKEN"},
"another-rest-api": {"USERNAME", "PASSWORD"},
"neon": {"POSTGRESQL_USERNAME", "POSTGRESQL_PASSWORD"},
"my-dgraph-cloud": {"DGRAPH_KEY"},
}

m, err := manifest.ReadManifest(validManifest)
Expand Down
5 changes: 5 additions & 0 deletions test/valid_hypermode.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"neon": {
"type": "postgresql",
"connString": "postgresql://{{POSTGRESQL_USERNAME}}:{{POSTGRESQL_PASSWORD}}@1.2.3.4:5432/data?sslmode=disable"
},
"my-dgraph-cloud": {
"type": "dgraph-cloud",
"endpoint": "https://frozen-mango.eu-central-1.aws.cloud.dgraph.io/graphql",
"key": "{{DGRAPH_KEY}}"
}
},
"collections": {
Expand Down