From b7813b875290b70d6c42408c2cb187da584667e8 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Sat, 4 Feb 2023 02:25:53 +0200 Subject: [PATCH] add inventory package Signed-off-by: Boris Glimcher --- Dockerfile | 4 +- goopicsi.go => pkg/csi/goopicsi.go | 4 +- goopicsi_test.go => pkg/csi/goopicsi_test.go | 4 +- pkg/inventory/inventory.go | 61 ++++++++++++++++++++ pkg/inventory/inventory_test.go | 4 ++ 5 files changed, 71 insertions(+), 6 deletions(-) rename goopicsi.go => pkg/csi/goopicsi.go (99%) rename goopicsi_test.go => pkg/csi/goopicsi_test.go (98%) create mode 100644 pkg/inventory/inventory.go create mode 100644 pkg/inventory/inventory_test.go diff --git a/Dockerfile b/Dockerfile index 9f0505d..7bc4ea1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,5 +12,5 @@ COPY go.sum ./ RUN go mod download # build an app -COPY *.go ./ -RUN go build -v +COPY pkg pkg +RUN go build -v ./... diff --git a/goopicsi.go b/pkg/csi/goopicsi.go similarity index 99% rename from goopicsi.go rename to pkg/csi/goopicsi.go index 877929b..f3f9103 100644 --- a/goopicsi.go +++ b/pkg/csi/goopicsi.go @@ -1,8 +1,8 @@ // SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2022 Dell Inc, or its subsidiaries. -// Package godpu implements the go library for OPI to be used in CSI drivers -package godpu +// Package csi implements the go library for OPI to be used in CSI drivers +package csi import ( "context" diff --git a/goopicsi_test.go b/pkg/csi/goopicsi_test.go similarity index 98% rename from goopicsi_test.go rename to pkg/csi/goopicsi_test.go index 88e2fd5..57127b6 100644 --- a/goopicsi_test.go +++ b/pkg/csi/goopicsi_test.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2022 Dell Inc, or its subsidiaries. -package godpu +package csi import ( "fmt" @@ -32,7 +32,7 @@ func RunServer() { fmt.Println("RUNNING MOCK SERVER") const ( csiAddress = "localhost:50051" - defaultStubsPath = "test/mock-server/stubs" + defaultStubsPath = "../../test/mock-server/stubs" apiPort = "4771" ) diff --git a/pkg/inventory/inventory.go b/pkg/inventory/inventory.go new file mode 100644 index 0000000..3055c66 --- /dev/null +++ b/pkg/inventory/inventory.go @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022 Dell Inc, or its subsidiaries. + +// Package inventory implements the go library for OPI to be used to query inventory +package inventory + +import ( + "context" + "log" + "time" + + pb "github.com/opiproject/opi-api/common/v1/gen/go" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +var ( + conn *grpc.ClientConn + address = "localhost:50051" +) + +// Get returns inventory information from DPUs +func Get() error { + if conn == nil { + err := dialConnection() + if err != nil { + return err + } + } + + client := pb.NewInventorySvcClient(conn) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + data, err := client.InventoryGet(ctx, &pb.InventoryGetRequest{}) + if err != nil { + log.Println(err) + } + log.Println(data) + defer disconnectConnection() + return nil +} + +func dialConnection() error { + var err error + conn, err = grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + log.Printf("Failed to connect: %v", err) + return err + } + return nil +} + +func disconnectConnection() { + err := conn.Close() + if err != nil { + log.Fatalf("Failed to close connection: %v", err) + } else { + log.Println("GRPC connection closed successfully") + } +} diff --git a/pkg/inventory/inventory_test.go b/pkg/inventory/inventory_test.go new file mode 100644 index 0000000..eb166ea --- /dev/null +++ b/pkg/inventory/inventory_test.go @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2022 Dell Inc, or its subsidiaries. + +package inventory