Skip to content

Commit

Permalink
ENG-7133 add EC2 endpoint (v2) (#25)
Browse files Browse the repository at this point in the history
## Description

<!--- Please describe what this PR is going to change -->
Adds the EC2 endpoint `/2009-04-04`.
This is v2 of #22.

## Why is this needed

<!--- Link to issue you have raised -->
There has been an effort to have Hegel succeed Kant, and one of the endpoints that Kant has (and Hegel doesn't) is the `/2009-04-04` endpoint. 

Fixes: #

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->
- Unit & integration tests
- Manual test with tinkerbell setup


## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->
No migration steps needed.

## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [x] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
mergify[bot] authored Aug 19, 2020
2 parents 9c4fc2f + 91c9f84 commit fa897aa
Show file tree
Hide file tree
Showing 8 changed files with 663 additions and 90 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/packethost/hegel
go 1.13

require (
github.com/docker/go-metrics v0.0.1 // indirect
github.com/golang/protobuf v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
Expand All @@ -12,6 +11,6 @@ require (
github.com/packethost/pkg v0.0.0-20190715213007-7c3a64b4b5e3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.5.1
github.com/tinkerbell/tink v0.0.0-20200724140154-850584d46c8d
github.com/tinkerbell/tink v0.0.0-20200807143153-f5831e4a5fb8
google.golang.org/grpc v1.29.1
)
20 changes: 11 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v1.4.2-0.20191212201129-5f9f41018e9d/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -61,13 +60,16 @@ github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:x
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
Expand Down Expand Up @@ -98,8 +100,6 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kdeng3849/tink v0.0.0-20200713034415-dd6d5ea8d040 h1:YTVq8u523XC/5Ru1+3uVcqzrZLcBm+KCyx1bATR90+w=
github.com/kdeng3849/tink v0.0.0-20200713034415-dd6d5ea8d040/go.mod h1:76mIaisvbix90uCd7nRXXNO198WiL+TeJihDr4BXRas=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down Expand Up @@ -207,12 +207,8 @@ github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/tebeka/strftime v0.1.3 h1:5HQXOqWKYRFfNyBMNVc9z5+QzuBtIXy03psIhtdJYto=
github.com/tebeka/strftime v0.1.3/go.mod h1:7wJm3dZlpr4l/oVK0t1HYIc4rMzQ2XJlOMIUJUJH6XQ=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tinkerbell/tink v0.0.0-20200708073808-997394055483 h1:tvPuScVBrNizM2+i3qj2+ZDD8EePx/UHohJMY/XiP4A=
github.com/tinkerbell/tink v0.0.0-20200708073808-997394055483/go.mod h1:fVMM7v8aqMiptIq3DZWleonNo30JIBA4CX7gT0vDaiU=
github.com/tinkerbell/tink v0.0.0-20200710050004-a68bec0e8c1b h1:J8RDGhwHOoMgD7M+IumyyUpBameMygH5qUqfJMDUZLI=
github.com/tinkerbell/tink v0.0.0-20200710050004-a68bec0e8c1b/go.mod h1:fVMM7v8aqMiptIq3DZWleonNo30JIBA4CX7gT0vDaiU=
github.com/tinkerbell/tink v0.0.0-20200724140154-850584d46c8d h1:n8Z9XVLfObhWMLnCvx7kf1KtuigdnU/EULDMHgQ6ILI=
github.com/tinkerbell/tink v0.0.0-20200724140154-850584d46c8d/go.mod h1:LCMa/UyQYNA0Tf6E26+94gjpy5jA8k0IM1fNryFfGns=
github.com/tinkerbell/tink v0.0.0-20200807143153-f5831e4a5fb8 h1:DGRtLnmp8nHAjj335oxZSBMZwGKq3zTF4Ukhg1Dk2tY=
github.com/tinkerbell/tink v0.0.0-20200807143153-f5831e4a5fb8/go.mod h1:HwV/Tvp3emVQqdHFuVhWvX7rQIODBCSbnz4PIsSb9v4=
go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
go.mongodb.org/mongo-driver v1.1.2/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
go.uber.org/atomic v1.2.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down Expand Up @@ -291,6 +287,8 @@ google.golang.org/genproto v0.0.0-20190708153700-3bdd9d9f5532/go.mod h1:z3L6/3dT
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 h1:fiNLklpBwWK1mth30Hlwk+fcdBmIALlgF5iy77O37Ig=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/grpc v1.12.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
Expand All @@ -304,8 +302,12 @@ google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
49 changes: 29 additions & 20 deletions grpc_server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"math"
"net"
Expand All @@ -18,6 +18,7 @@ import (
"github.com/packethost/cacher/protos/cacher"
"github.com/packethost/hegel/grpc/hegel"
"github.com/packethost/hegel/metrics"
"github.com/pkg/errors"
tink "github.com/tinkerbell/tink/protos/hardware"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
Expand Down Expand Up @@ -177,7 +178,7 @@ func exportHardware(hw []byte) ([]byte, error) {
}

func filterMetadata(hw []byte, filter string) ([]byte, error) {
var result interface{}
var result bytes.Buffer
query, err := gojq.Parse(filter)
if err != nil {
return nil, err
Expand All @@ -193,19 +194,27 @@ func filterMetadata(hw []byte, filter string) ([]byte, error) {
if !ok {
break
}
if err, ok := v.(error); ok {
return nil, err

if v == nil {
continue
}
result = v
}

if resultString, ok := result.(string); ok { // if already a string, don't marshal
return []byte(resultString), nil
}
if result != nil { // if nil, don't marshal (json.Marshal(nil) returns "null")
return json.Marshal(result)
switch vv := v.(type) {
case error:
return nil, errors.Wrap(vv, "error while filtering with gojq")
case string:
result.WriteString(vv)
default:
marshalled, err := json.Marshal(vv)
if err != nil {
return nil, errors.Wrap(err, "error marshalling jq result")
}
result.Write(marshalled)
}
result.WriteRune('\n')
}
return nil, nil

return bytes.TrimSuffix(result.Bytes(), []byte("\n")), nil
}

// UnmarshalJSON implements the json.Unmarshaler interface for custom unmarshalling of exportedHardwareCacher
Expand Down Expand Up @@ -415,20 +424,21 @@ func getByIP(ctx context.Context, s *server, userIP string) ([]byte, error) {
req := &tink.GetRequest{
Ip: userIP,
}
resp, err := s.hardwareClient.ByIP(ctx, req) // use wrapper?
resp, err := s.hardwareClient.ByIP(ctx, req)

if err != nil {
return nil, err
}

if resp == nil {
return nil, errors.New("could not find hardware")
}

hw, err = json.Marshal(util.HardwareWrapper{Hardware: resp.(*tink.Hardware)})
if err != nil {
return nil, errors.New("could not marshal hardware")
}

if string(hw) == "{}" {
return nil, errors.New("could not find hardware")
}

default:
req := &cacher.GetRequest{
IP: userIP,
Expand All @@ -439,11 +449,10 @@ func getByIP(ctx context.Context, s *server, userIP string) ([]byte, error) {
return nil, err
}

if resp == nil {
hw = []byte(resp.(*cacher.Hardware).JSON)
if string(hw) == "" {
return nil, errors.New("could not find hardware")
}

hw = []byte(resp.(*cacher.Hardware).JSON)
}

return hw, nil
Expand Down
Loading

0 comments on commit fa897aa

Please sign in to comment.