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

Refactor hub-js, add value mirroring for specified backend #652

Merged

Conversation

mszostok
Copy link
Member

@mszostok mszostok commented Mar 1, 2022

Description

Changes proposed in this pull request:

  • Refactor hub-js,
    • add prettier and eslint (also enable check on CI)
    • split code to dedicated pkgs/files
    • code hardening
  • Add value mirroring for specified backend. With proper rollback in case of any error during transaction.

    NOTE: Because get query was not changed, I still need to save each value into built-in storage. In the follow-up PR I will add support for get/update/delete/lock/unlock and also remove storing value in built-in storage when not needed.

  • Add gRPC client service in Local Hub
  • Remove duplicated code for createTypeInstance and instead delegate it to createTypeInstances mutation. Because of that I had to change the return type from TypeInstance to { id, alias}
  • Update Local Hub GraphQL mutations and queries with backend.context property. I updated Go Client, GraphQL, Local Hub, examples to use new syntax:
    • Mutation
      mutation CreateTypeInstances {
        createTypeInstances(
          in: {
           typeInstances: [
            {
              alias: "helm-release"
              typeRef: { path: "cap.type.helm.chart.release", revision: "0.1.0" }
              value: {
                key: "test" # same as it was
              }
              backend: {
                id: "123" # it's like that already.
                context: { # new property of type `Any!`
                  name: "release-name",
                  namespace: "release-namespace",
                }
              }
            }
          ]
        }
        ) {
          id
          alias
        }
      }
    • Query
      query GetTypeInstance($typeInstanceID: ID!) {
        typeInstance(id: $typeInstanceID) {
          id
          backend { # current backend entry
            id
            abstract
          }    
          latestResourceVersion {
              resourceVersion
              spec {
                value
              }
              backend {
                context { # new property of type `Any!`
                  name
                  namespace
                }
              }
          }
        }
      }

Testing

The test that I wrote, proves that it works e2e (Go client -> GraphQL -> Local Hub -> External Storage).

  1. Run neo4j:

    docker run -d \
      -p 7687:7687 -p 7474:7474 \
      -e "NEO4J_AUTH=neo4j/okon" \
      -e "NEO4JLABS_PLUGINS=[\"apoc\"]" \
      --name hub-neo4j-instance \
      ghcr.io/capactio/neo4j:4.2.13-apoc
  2. Run modified storage server:

    APP_LOGGER_LEVEL="debug" APP_LOGGER_DEV_MODE=true APP_SUPPORTED_PROVIDERS="dotenv" go run ./cmd/secret-storage-backend/main.go
  3. Run Local Hub:

    cd hub-js; APP_NEO4J_ENDPOINT=bolt://localhost:7687 APP_NEO4J_PASSWORD=okon APP_HUB_MODE=local npm run dev; cd ..
  4. Run test:

    GRPC_SECRET_STORAGE_BACKEND_ADDR="0.0.0.0:50051" go test ./pkg/hub/client/local/ -v -count 1

    You should get similar output:

    === RUN   TestThatShowcaseExternalStorage
                TYPE INSTANCE ID                ALIAS                TYPE                  DATA FROM GQL                       BACKEND                           BACKEND CONTEXT            DATA IN EXTERNAL BACKEND
    ---------------------------------------+--------------+------------------------+---------------------------+--------------------------------------+-----------------------------------+---------------------------
      84d521b9-415e-47cd-b927-c8d97daaac62   second-child   cap.type.child:0.2.0     {"name":"Leia Organa"}      5f7505bc-ba83-4a83-a8fe-16127944dc8d   {"modified":"by-backend-storage"}   {"name":"Leia Organa"}
    ---------------------------------------+--------------+------------------------+---------------------------+--------------------------------------+-----------------------------------+---------------------------
      47897035-150b-4d5b-8d95-26c279aed72d   child          cap.type.child:0.1.0     {"name":"Luke Skywalker"}   318b99bd-9b26-4bc1-8259-0a7ff5dae61c   null
                                                                                                                 (abstract)
    ---------------------------------------+--------------+------------------------+---------------------------+--------------------------------------+-----------------------------------+---------------------------
      e9eec931-63b8-48c4-80a8-ff1dde970368   parent         cap.type.complex:0.1.0   {"name":"Darth Vader"}      5f7505bc-ba83-4a83-a8fe-16127944dc8d   {"provider":"dotenv"}               {"name":"Darth Vader"}
    ---------------------------------------+--------------+------------------------+---------------------------+--------------------------------------+-----------------------------------+---------------------------
    --- PASS: TestThatShowcaseExternalStorage (3.26s)
    PASS
    ok  	capact.io/capact/pkg/hub/client/local	3.544s
  5. Manually remove dotenv storage data: rm -rf /tmp/capact

  6. Modify the relations in test /pkg/hub/client/local/client_test.go from:

    	    UsesRelations: []*gqllocalapi.TypeInstanceUsesRelationInput{
    		    {From: "parent", To: "child"},
    		    {From: "parent", To: "second-child"},
    	    },

    to

    	    UsesRelations: []*gqllocalapi.TypeInstanceUsesRelationInput{
    		    {From: "parent", To: "child"},
    		    {From: "parent", To: "raise-an-error-child"},
    	    },
  7. Run tests:

    GRPC_SECRET_STORAGE_BACKEND_ADDR="0.0.0.0:50051" go test ./pkg/hub/client/local/ -v -count 1

    You should observe an error in transaction after storing data in external system, but the related data should be automatically deleted in this case, leaving the system in the expected state.

  8. Check that no data is stored: ls /tmp/capact

Related issue(s)

@mszostok mszostok added enhancement New feature or request WIP Work in progress area/hub Relates to Hub labels Mar 1, 2022
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch from 18c7b51 to 644a294 Compare March 2, 2022 17:34
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch from 644a294 to 88d46fe Compare March 2, 2022 17:48
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch 2 times, most recently from b6e6e6d to 73e315b Compare March 2, 2022 23:03
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch from 73e315b to 45c2817 Compare March 2, 2022 23:33
@mszostok mszostok removed the WIP Work in progress label Mar 3, 2022
@pkosiec pkosiec self-assigned this Mar 3, 2022
Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good work! I have a few comments regarding app behavior and project structure.

hub-js/tsconfig.json Outdated Show resolved Hide resolved
hub-js/tsconfig.json Outdated Show resolved Hide resolved
hub-js/package.json Outdated Show resolved Hide resolved
hub-js/package.json Outdated Show resolved Hide resolved
hack/lint.sh Show resolved Hide resolved
hub-js/src/local/mutation/toggle-lock-type-instances.ts Outdated Show resolved Hide resolved
hub-js/src/local/mutation/create-type-instances.ts Outdated Show resolved Hide resolved
hub-js/src/local/mutation/create-type-instance.ts Outdated Show resolved Hide resolved
pkg/hub/client/local/client_test.go Outdated Show resolved Hide resolved
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch 2 times, most recently from ed5701b to 92ee949 Compare March 3, 2022 18:50
@capactio capactio deleted a comment from github-actions bot Mar 3, 2022
@capactio capactio deleted a comment from github-actions bot Mar 3, 2022
@capactio capactio deleted a comment from github-actions bot Mar 3, 2022
@capactio capactio deleted a comment from github-actions bot Mar 3, 2022
@capactio capactio deleted a comment from github-actions bot Mar 3, 2022
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch 4 times, most recently from 3251814 to ed3b090 Compare March 3, 2022 20:58
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch from ed3b090 to d1fcb7c Compare March 3, 2022 21:03
@capactio capactio deleted a comment from github-actions bot Mar 4, 2022
Copy link
Member

@pkosiec pkosiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 Before merge, please just address one last comment from my side 🙂

hub-js/src/local/mutation/create-type-instances.ts Outdated Show resolved Hide resolved
@mszostok mszostok force-pushed the hub-grpc/clean-up-and-create-mutation branch from 3606971 to f6d5c1e Compare March 6, 2022 12:31
@capactio capactio deleted a comment from github-actions bot Mar 6, 2022
@mszostok mszostok merged commit 95643d2 into capactio:main Mar 6, 2022
@mszostok mszostok deleted the hub-grpc/clean-up-and-create-mutation branch March 6, 2022 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hub Relates to Hub enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants