Skip to content

Commit

Permalink
feat: onboard Equinix Metal client (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma authored Dec 12, 2023
2 parents df211b6 + cbfa94f commit a082595
Show file tree
Hide file tree
Showing 1,476 changed files with 225,284 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/sync-metalv1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Sync metalv1 API spec

on:
workflow_dispatch:

jobs:
sync:
strategy:
matrix:
go-version: [1.19.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Get current date
id: date
run: echo "date=$(date +'%Y-%m-%d')" >> "$GITHUB_OUTPUT"
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v4
- name: GitHub user
run: |
# https://api.github.com/users/github-actions[bot]
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
- name: Fetch latest spec
id: fetch
run: |
make -f Makefile.metalv1 fetch
git add spec/services/metalv1
echo `git commit -m 'sync: fetch ${{ steps.date.outputs.date }} spec and apply patches'`
- name: Apply spec patches
id: patch
if: ${{ always() && steps.fetch.conclusion == 'success' }}
run: |
make -f Makefile.metalv1 patch
git add spec/services/metalv1
echo `git commit -m 'sync: patch spec with ${{ steps.date.outputs.date }} spec'`
- name: Generate code
id: generate
if: ${{ always() && steps.patch.conclusion == 'success' }}
run: |
make -f Makefile.metalv1 generate
git add services/metalv1
echo `git commit -m 'sync: generate client with ${{ steps.date.outputs.date }} spec'`
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v5
if: ${{ always() && steps.fetch.conclusion == 'success' }}
with:
branch: sync/gh
branch-suffix: timestamp
author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
commit-message: "sync: uncommitted changes detected when opening PR"
title: "feat: API Sync by GitHub Action for @${{ github.event.sender.login }}"
body: |
This API Sync PR was triggered by @${{ github.event.sender.login }} through [GitHub Actions workflow_displatch](https://github.com/equinix/equinix-sdk-go/actions?query=event%3Aworkflow_dispatch)
on ${{ steps.date.outputs.date }}.
* latest Swagger is fetched
* patches have been applied
* generated client has been updated
delete-branch: true
draft: ${{ steps.patch.conclusion == 'failure' || steps.generate.conclusion == 'failure' }}
- name: Comment for failed patch
uses: mshick/add-pr-comment@v2
if: ${{ always() && steps.patch.conclusion == 'failure' && steps.cpr.conclusion == 'success' }}
with:
issue: ${{ steps.cpr.outputs.pull-request-number }}
message: Failed to patch latest spec. Someone with write access must fix this PR manually and then convert it from Draft status to Ready for Review.
- name: Comment for failed generate
uses: mshick/add-pr-comment@v2
if: ${{ always() && steps.generate.conclusion == 'failure' && steps.cpr.conclusion == 'success' }}
with:
issue: ${{ steps.cpr.outputs.pull-request-number }}
message: Failed to generate code from latest patched spec. Someone with write access must fix this PR manually and then convert it from Draft status to Ready for Review.
- name: Check outputs
if: ${{ always() && steps.cpr.conclusion == 'success' }}
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
31 changes: 31 additions & 0 deletions .github/workflows/test-metalv1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test metalv1 codegen

on:
push:
paths:
- "**/metalv1/**"
pull_request:
paths:
- "**/metalv1/**"

jobs:
test:
strategy:
matrix:
go-version: [1.19.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v4
- name: Verify Patches
run: make -f Makefile.metalv1 patch
- name: Generate
run: |
make -f Makefile.metalv1 generate
# Expect all changes to be accounted for
! git status --porcelain | grep .
65 changes: 65 additions & 0 deletions Makefile.metalv1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.PHONY: all pull fetch patch generate clean codegen mod docs move-other patch-post fmt test stage

include Makefile

PACKAGE_NAME=metalv1
SPEC_BASE_URL:=https://api.equinix.com/metal/v1/api-docs
SPEC_ROOT_FILE:=openapi3.yaml

CODE_DIR=${CODE_BASE_DIR}/${PACKAGE_NAME}
TEMPLATE_DIR=${TEMPLATE_BASE_DIR}/${PACKAGE_NAME}
SPEC_FETCHED_DIR=${SPEC_BASE_DIR}/${PACKAGE_NAME}/oas3.fetched
SPEC_PATCH_DIR=${SPEC_BASE_DIR}/${PACKAGE_NAME}/patches
SPEC_PATCHED_DIR=${SPEC_BASE_DIR}/${PACKAGE_NAME}/oas3.patched

all: pull fetch patch generate stage

generate: clean codegen remove-unused mod patch-post fmt test

pull:
${CRI} pull ${OPENAPI_IMAGE}

fetch:
${SPEC_FETCHER} ${SPEC_BASE_URL} ${SPEC_FETCHED_DIR} ${SPEC_ROOT_FILE}

patch:
rm -rf ${SPEC_PATCHED_DIR}
cp -r ${SPEC_FETCHED_DIR} ${SPEC_PATCHED_DIR}

for diff in $(shell set -x; find ${SPEC_PATCH_DIR} -name '*.patch' | sort -n); do \
patch --no-backup-if-mismatch -N -t -p1 -i $$diff; \
done

patch-post:
# patch is idempotent, always starting with the generated files
for diff in $(shell find patches/post -name \*.patch | sort -n); do \
patch --no-backup-if-mismatch -N -t -p1 -i $$diff; \
done

clean:
rm -rf $(CODE_DIR)

codegen:
${OPENAPI_GENERATOR} generate -g go \
--package-name ${PACKAGE_NAME} \
--http-user-agent "${USER_AGENT}" \
-p packageVersion=${PACKAGE_VERSION} \
--git-user-id ${GIT_ORG} \
--git-repo-id ${GIT_REPO}/services \
-c /local/config/openapi-generator.json \
-t /local/${TEMPLATE_DIR} \
-o /local/${CODE_DIR} \
-i /local/${SPEC_PATCHED_DIR}/${SPEC_ROOT_FILE}

validate:
${OPENAPI_GENERATOR} validate \
--recommend \
-i /local/${SPEC_PATCHED_DIR}

remove-unused:
rm -rf ${CODE_DIR}/api \
${CODE_DIR}/.travis.yml \
${CODE_DIR}/git_push.sh \
${CODE_DIR}/.openapi-generator \
${CODE_DIR}/go.mod \
${CODE_DIR}/go.sum
Empty file added examples/.keep
Empty file.
10 changes: 10 additions & 0 deletions examples/services/metalv1/create_device_in_facility/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/equinix/equinix-sdk-go/examples/service/metalv1/create_device_in_facility

go 1.16

require (
github.com/equinix/equinix-sdk-go v0.0.0-00010101000000-000000000000
sigs.k8s.io/yaml v1.2.0
)

replace github.com/equinix/equinix-sdk-go => ../../../..
18 changes: 18 additions & 0 deletions examples/services/metalv1/create_device_in_facility/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
42 changes: 42 additions & 0 deletions examples/services/metalv1/create_device_in_facility/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"flag"
"fmt"
"os"

metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"sigs.k8s.io/yaml"
)

func main() {
var projectId string
flag.StringVar(&projectId, "projectId", "", "")
flag.Parse()

configuration := metal.NewConfiguration()
configuration.Debug = true
configuration.AddDefaultHeader("X-Auth-Token", os.Getenv("METAL_AUTH_TOKEN"))
api_client := metal.NewAPIClient(configuration)
deviceRequest := metal.CreateDeviceRequest{
DeviceCreateInFacilityInput: &metal.DeviceCreateInFacilityInput{
Facility: []string{"fr2"},
Plan: "c3.medium.x86",
OperatingSystem: "ubuntu_20_04",
},
}
device, r, err := api_client.DevicesApi.CreateDevice(context.Background(), projectId).CreateDeviceRequest(deviceRequest).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.CreateDevice`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}

// response from `CreateDevice`: Device
s, err := yaml.Marshal(device)
if err != nil {
panic(err)
}

fmt.Fprintf(os.Stdout, "Response from `PlansApi.FindPlans`: %v\n", string(s))
}
10 changes: 10 additions & 0 deletions examples/services/metalv1/create_device_in_metro/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/equinix/equinix-sdk-go/examples/service/metalv1/create_device_in_metro

go 1.16

require (
github.com/equinix/equinix-sdk-go v0.1.1-0.20220706151426-4a8e1963e004
sigs.k8s.io/yaml v1.2.0
)

replace github.com/equinix/equinix-sdk-go => ../../../..
18 changes: 18 additions & 0 deletions examples/services/metalv1/create_device_in_metro/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
42 changes: 42 additions & 0 deletions examples/services/metalv1/create_device_in_metro/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"flag"
"fmt"
"os"

metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"sigs.k8s.io/yaml"
)

func main() {
var projectId string
flag.StringVar(&projectId, "projectId", "", "")
flag.Parse()

configuration := metal.NewConfiguration()
configuration.Debug = true
configuration.AddDefaultHeader("X-Auth-Token", os.Getenv("METAL_AUTH_TOKEN"))
api_client := metal.NewAPIClient(configuration)
deviceRequest := metal.CreateDeviceRequest{
DeviceCreateInMetroInput: &metal.DeviceCreateInMetroInput{
Metro: "da",
Plan: "c3.medium.x86",
OperatingSystem: "ubuntu_20_04",
},
}
device, r, err := api_client.DevicesApi.CreateDevice(context.Background(), projectId).CreateDeviceRequest(deviceRequest).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.CreateDevice`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}

// response from `CreateDevice`: Device
s, err := yaml.Marshal(device)
if err != nil {
panic(err)
}

fmt.Fprintf(os.Stdout, "Response from `PlansApi.FindPlans`: %v\n", string(s))
}
10 changes: 10 additions & 0 deletions examples/services/metalv1/plans/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/equinix/equinix-sdk-go/examples/service/metalv1/plans

go 1.16

require (
github.com/equinix/equinix-sdk-go v0.1.1-0.20220706151426-4a8e1963e004
sigs.k8s.io/yaml v1.2.0
)

replace github.com/equinix/equinix-sdk-go => ../../../..
18 changes: 18 additions & 0 deletions examples/services/metalv1/plans/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
36 changes: 36 additions & 0 deletions examples/services/metalv1/plans/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"fmt"
"os"

metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"sigs.k8s.io/yaml"
)

func main() {
// TODO Because the swagger2 spec can not declare that facilities can be
// returned with plan results, including available_in results in:
// available_in:
// - href: ""
include := []string{}
exclude := []string{"available_in"}
configuration := metal.NewConfiguration()
configuration.Debug = true
configuration.AddDefaultHeader("X-Auth-Token", os.Getenv("METAL_AUTH_TOKEN"))
api_client := metal.NewAPIClient(configuration)
resp, r, err := api_client.PlansApi.FindPlans(context.Background()).Include(include).Exclude(exclude).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `PlansApi.FindPlans``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}

// response from `FindPlans`: PlanList
s, err := yaml.Marshal(resp.Plans)
if err != nil {
panic(err)
}

fmt.Fprintf(os.Stdout, "Response from `PlansApi.FindPlans`: %v\n", string(s))
}
Empty file added patches/.keep
Empty file.
Empty file added patches/post/.keep
Empty file.
Empty file added patches/services/metalv1/.keep
Empty file.
Loading

0 comments on commit a082595

Please sign in to comment.