Skip to content

Commit

Permalink
Inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuedding committed Mar 17, 2024
1 parent 3c8d0f5 commit a0b8234
Show file tree
Hide file tree
Showing 10 changed files with 1,124 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'

name: Create Release
jobs:
build:
permissions: write-all
name: Create Release
runs-on: ubuntu-latest
steps:
- uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false

#- uses: actions/checkout@v3
#- name: Create a Release
# uses: elgohr/Github-Release-Action@v5
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# title: ${{github.ref_name}}
#- name: Latest tag
# # You may pin to the exact commit or the version.
# uses: EndBug/[email protected]
# with:
# # Name of the tag or branch to update
# ref: ${{ github.ref }}
# # Tag name
# #tag-name: # optional
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/fbuedding/fiware-iot-agent-sdk

go 1.21.1

require (
github.com/niemeyer/golang v0.0.0-20110826170342-f8c0f811cb19
github.com/rs/zerolog v1.32.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/niemeyer/golang v0.0.0-20110826170342-f8c0f811cb19 h1:HDsKR+rtTZD+ey3J3U+UWJLko6XJkNWaRp0+yn1u0FQ=
github.com/niemeyer/golang v0.0.0-20110826170342-f8c0f811cb19/go.mod h1:vOsSoNMQygvjuK93uIN1lif16OAgfRDlVu7hHS96lPg=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
300 changes: 300 additions & 0 deletions iota-config-group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
package iotagentsdk

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"

"github.com/niemeyer/golang/src/pkg/container/vector"
"github.com/rs/zerolog/log"
)

const (
urlService = urlBase + "/iot/services"
)

func (e *MissingFields) Error() string {
return fmt.Sprintf("Error %s: %s", e.Message, e.Fields)
}

func (sg ConfigGroup) Validate() error {
mF := &MissingFields{make(vector.StringVector, 0), "Missing fields"}
if sg.Apikey == "" {
mF.Fields.Push("Apikey")
}
if sg.Resource == "" {
mF.Fields.Push("Resource")
}

if mF.Fields.Len() == 0 {
return nil
} else {
return mF
}
}

type RespReadConfigGroup struct {
Count int `json:"count"`
Services []ConfigGroup `json:"services"`
}

type ReqCreateConfigGroup struct {
Services []ConfigGroup `json:"services"`
}

func (i IoTA) ReadConfigGroup(fs FiwareService, r Resource, a Apikey) (*RespReadConfigGroup, error) {
url := urlService + fmt.Sprintf("?resource=%s&apikey=%s", r, a)

method := "GET"

client := i.Client()
req, err := http.NewRequest(method, fmt.Sprintf(url, i.Host, i.Port), nil)
if err != nil {
return nil, fmt.Errorf("Error while getting service: %w", err)
}
req.Header.Add("fiware-service", fs.Service)
req.Header.Add("fiware-servicepath", fs.ServicePath)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error while getting service: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
resData, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Error while eding response body %w", err)
}
var apiError ApiError
json.Unmarshal(resData, &apiError)
return nil, apiError
}

responseData, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Error while getting service: %w", err)
}

var respReadConfigGroup RespReadConfigGroup
json.Unmarshal(responseData, &respReadConfigGroup)
return &respReadConfigGroup, nil
}

func (i IoTA) ListConfigGroups(fs FiwareService) (*RespReadConfigGroup, error) {
url := urlService

method := "GET"

client := i.Client()
req, err := http.NewRequest(method, fmt.Sprintf(url, i.Host, i.Port), nil)
if err != nil {
return nil, fmt.Errorf("Error while getting service: %w", err)
}
req.Header.Add("fiware-service", fs.Service)
req.Header.Add("fiware-servicepath", fs.ServicePath)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error while getting service: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
resData, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Error while eding response body %w", err)
}
var apiError ApiError
json.Unmarshal(resData, &apiError)
return nil, apiError
}

responseData, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Error while getting service: %w", err)
}

var respReadConfigGroup RespReadConfigGroup
json.Unmarshal(responseData, &respReadConfigGroup)
return &respReadConfigGroup, nil
}

func (i IoTA) ConfigGroupExists(fs FiwareService, r Resource, a Apikey) bool {
tmp, err := i.ReadConfigGroup(fs, r, a)
if err != nil {
return false
}
return tmp.Count > 0
}

func (i IoTA) CreateConfigGroup(fs FiwareService, sg ConfigGroup) error {
sgs := [1]ConfigGroup{sg}
return i.CreateConfigGroups(fs, sgs[:])
}

func (i IoTA) CreateConfigGroups(fs FiwareService, sgs []ConfigGroup) error {
for _, sg := range sgs {
err := sg.Validate()
if err != nil {
return err
}
}
reqCreateConfigGroup := ReqCreateConfigGroup{}
reqCreateConfigGroup.Services = sgs[:]
method := "POST"

payload, err := json.Marshal(reqCreateConfigGroup)
if err != nil {
log.Panic().Err(err).Msg("Could not Marshal struct")
}
client := i.Client()
req, err := http.NewRequest(method, fmt.Sprintf(urlService, i.Host, i.Port), bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("Error while creating Request %w", err)
}
req.Header.Add("fiware-service", fs.Service)
req.Header.Add("fiware-servicepath", fs.ServicePath)
req.Header.Add("Content-Type", "application/json")

res, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error while requesting resource %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusCreated {
resData, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("Error while eding response body %w", err)
}
var apiError ApiError
json.Unmarshal(resData, &apiError)
return apiError
}

return nil
}

func (i IoTA) UpdateConfigGroup(fs FiwareService, r Resource, a Apikey, sg ConfigGroup) error {
err := sg.Validate()
if err != nil {
return err
}
url := urlService + fmt.Sprintf("?resource=%s&apikey=%s", r, a)
method := "PUT"

payload, err := json.Marshal(sg)
if err != nil {
log.Panic().Err(err).Msg("Could not Marshal struct")
}
if string(payload) == "{}" {
return nil
}
client := i.Client()
req, err := http.NewRequest(method, fmt.Sprintf(url, i.Host, i.Port), bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("Error while creating Request %w", err)
}
req.Header.Add("fiware-service", fs.Service)
req.Header.Add("fiware-servicepath", fs.ServicePath)
req.Header.Add("Content-Type", "application/json")

res, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error while requesting resource %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusNoContent {
resData, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("Error while eding response body %w", err)
}
var apiError ApiError
json.Unmarshal(resData, &apiError)
return apiError
}

return nil
}

func (i IoTA) DeleteConfigGroup(fs FiwareService, r Resource, a Apikey) error {
url := urlService + fmt.Sprintf("?resource=%s&apikey=%s", r, a)

method := http.MethodDelete

client := http.Client{}
req, err := http.NewRequest(method, fmt.Sprintf(url, i.Host, i.Port), strings.NewReader(""))
if err != nil {
return fmt.Errorf("Error while creating Request %w", err)
}

req.Header.Add("fiware-service", fs.Service)
req.Header.Add("fiware-servicepath", fs.ServicePath)

res, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error while requesting resource %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusNoContent {
resData, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("Error while eding response body %w", err)
}
var apiError ApiError
json.Unmarshal(resData, &apiError)
return apiError
}

return nil
}

func (i IoTA) UpsertConfigGroup(fs FiwareService, sg ConfigGroup) error {
exists := i.ConfigGroupExists(fs, sg.Resource, sg.Apikey)
if !exists {
log.Debug().Msg("Creating service group...")
err := i.CreateConfigGroup(fs, sg)
if err != nil {
return err
}
} else {
log.Debug().Msg("Update service group...")
err := i.UpdateConfigGroup(fs, sg.Resource, sg.Apikey, sg)
if err != nil {
return err
}
}
return nil
}

func (i IoTA) CreateConfigGroupWSE(fs FiwareService, sg *ConfigGroup) error {
if sg == nil {
return errors.New("Service group reference cannot be nil")
}

err := i.CreateConfigGroup(fs, *sg)
if err != nil {
return err
}

sgTmp, err := i.ReadConfigGroup(fs, sg.Resource, sg.Apikey)
if err != nil {
return err
}

if sgTmp.Count == 0 {
return errors.New("No service group created")
}
*sg = *&sgTmp.Services[0]

return nil
}
Loading

0 comments on commit a0b8234

Please sign in to comment.