Skip to content

Commit

Permalink
Merge pull request astarte-platform#268 from matt-mazzucato/fwport-fr…
Browse files Browse the repository at this point in the history
…om-24.5.1

Forward-port changes from 24.5.2 to master
  • Loading branch information
Annopaolo authored Sep 20, 2024
2 parents 0f48d39 + ed321b1 commit 0be8ab2
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 87 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [24.5.2] - 2024-09-20
### Fixed
- Allow a larger set of permissions for configuration files and folders.

## [24.5.1] - 2024-09-19
### Changed
- `cluster instance` {`deploy` | `destroy` | `show`} are deprecated and
will be removed from v24.11.

### Fixed
- Create configuration files and folders if required, instead of just crashing.
- Fixed empty array sent as empty string for array payload.
- Allow the management of triggers containing Mustache templates.

## [24.5.0] - 2024-09-03
### Added
- `cluster instance deploy`: add profiles for astarte v1.2.

Expand Down
43 changes: 22 additions & 21 deletions cmd/appengine/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"math"
"os"
"regexp"
"strconv"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -1649,34 +1650,34 @@ func parseSendDataPayload(payload string, mappingType interfaces.AstarteMappingT
case interfaces.BinaryBlobArray, interfaces.BooleanArray, interfaces.DateTimeArray, interfaces.DoubleArray,
interfaces.IntegerArray, interfaces.LongIntegerArray, interfaces.StringArray:

//wait it's all string?
payload = strings.Replace(payload, "[", "", -1)
payload = strings.Replace(payload, "]", "", -1)
payload_parsed := strings.Split(payload, ",")
//always has been

jsonOut := make([]interface{}, len(payload_parsed))
for i, v := range payload_parsed {
jsonOut[i] = v
}

retArray := []interface{}{}
// Do a smarter conversion here.
for _, v := range jsonOut {
switch val := v.(type) {
case string:
p, err := parseSendDataPayload(strings.TrimSpace(val), interfaces.AstarteMappingType(strings.TrimSuffix(string(mappingType), "array")))
//check if payload is an empty array, bypass conversions and just return an empty array
// if it is not, proceed as usual
if payload == "[]" {
ret = make([]interface{}, 0)
} else {
retArray := []interface{}{}
payload = strings.TrimSpace(payload)
if pass, err := regexp.MatchString(`^\[.*\]$`, payload); !pass {
return nil, err
}
payload = regexp.MustCompile(`^\[`).ReplaceAllString(payload, "")
payload = regexp.MustCompile(`\]$`).ReplaceAllString(payload, "")
payload_parsed := strings.Split(payload, ",")
jsonOut := make([]interface{}, len(payload_parsed))
for i, v := range payload_parsed {
jsonOut[i] = v
}
// Do a smarter conversion here.
for _, v := range jsonOut {
p, err := parseSendDataPayload(strings.TrimSpace(v.(string)), interfaces.AstarteMappingType(strings.TrimSuffix(string(mappingType), "array")))
if err != nil {
return nil, err
}
retArray = append(retArray, p)
default:
retArray = append(retArray, val)
}
ret = retArray
}
ret = retArray
}

return ret, nil
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/cluster/instance_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var deployCmd = &cobra.Command{
kubectl mentions. If no versions are specified, the last stable version is deployed. This should only be used for testing purposes.`,
Example: ` astartectl cluster instances deploy`,
RunE: clusterDeployF,
Deprecated: `This command is deprecated and will be removed in future releases.
Refer to the Astarte documentation on how to install Astarte on your cluster:
https://docs.astarte-platform.org/astarte-kubernetes-operator/latest`,
}

func init() {
Expand Down
3 changes: 3 additions & 0 deletions cmd/cluster/instance_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ kubectl mentions. Please be aware of the fact that when an Astarte instance is d
Example: ` astartectl cluster instances destroy astarte`,
RunE: clusterDestroyF,
Args: cobra.ExactArgs(1),
Deprecated: `This command is deprecated and will be removed in future releases.
Refer to the Astarte documentation on how to remove Astarte from your cluster:
https://docs.astarte-platform.org/astarte-kubernetes-operator/latest`,
}

func init() {
Expand Down
13 changes: 1 addition & 12 deletions cmd/cluster/instance_get_cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,7 @@ func doGetClusterConfig(resourceName, resourceNamespace string) error {
fmt.Printf("Created new Context %s\n", contextName)

// Now set the current context to the new one
baseConfig, err := config.LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = config.BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
}

baseConfig.CurrentContext = contextName
if err := config.SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
config.UpdateBaseConfigWithContext(configDir, contextName)
fmt.Printf("Context switched to %s\n", contextName)

return nil
Expand Down
3 changes: 3 additions & 0 deletions cmd/cluster/instance_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var instanceShowCmd = &cobra.Command{
Example: ` astartectl cluster instances show astarte`,
RunE: instanceShowF,
Args: cobra.ExactArgs(1),
Deprecated: `This command is deprecated and will be removed in future releases.
Refer to the Astarte documentation on how to interact with Astarte:
https://docs.astarte-platform.org/astarte-kubernetes-operator/latest`,
}

func init() {
Expand Down
6 changes: 1 addition & 5 deletions cmd/config/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ func contextsListF(command *cobra.Command, args []string) error {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
baseConfig, err := config.LoadBaseConfiguration(config.GetConfigDir())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
baseConfig := config.GetBaseConfig(configDir)

w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', tabwriter.DiscardEmptyColumns)
// header
Expand Down
28 changes: 4 additions & 24 deletions cmd/config/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,13 @@ func init() {
}

func currentContextF(command *cobra.Command, args []string) error {
baseConfig, err := config.LoadBaseConfiguration(config.GetConfigDir())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

baseConfig := config.GetBaseConfig(config.GetConfigDir())
fmt.Println(baseConfig.CurrentContext)
return nil
}

func currentClusterF(command *cobra.Command, args []string) error {
baseConfig, err := config.LoadBaseConfiguration(config.GetConfigDir())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

baseConfig := config.GetBaseConfig(config.GetConfigDir())
currentContext, err := config.LoadContextConfiguration(config.GetConfigDir(), baseConfig.CurrentContext)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -94,16 +84,6 @@ func setCurrentContextF(command *cobra.Command, args []string) error {
}

func updateCurrentContext(newCurrentContext string) error {
configDir := config.GetConfigDir()
baseConfig, err := config.LoadBaseConfiguration(configDir)
if err != nil {
return err
}
if _, err := config.LoadContextConfiguration(configDir, newCurrentContext); err != nil {
return err
}

baseConfig.CurrentContext = newCurrentContext

return config.SaveBaseConfiguration(configDir, baseConfig)
config.UpdateBaseConfigWithContext(config.GetConfigDir(), newCurrentContext)
return nil
}
15 changes: 1 addition & 14 deletions cmd/housekeeping/realms.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,7 @@ func realmsCreateF(command *cobra.Command, args []string) error {
fmt.Printf("Context %s created successfully\n", contextName)

// Now set the current context to the new one
baseConfig, err := config.LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = config.BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
}

baseConfig.CurrentContext = contextName
if err := config.SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "warn: Context not switched")
} else {
fmt.Printf("Context switched to %s\n", contextName)
}
config.UpdateBaseConfigWithContext(configDir, contextName)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/realm/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func getInterfaceDefinition(realm, interfaceName string, interfaceMajor int) (in
}

func installInterface(realm string, iface interfaces.AstarteInterface) error {
installInterfaceCall, err := astarteAPIClient.InstallInterface(realm, iface, true)
installInterfaceCall, err := astarteAPIClient.InstallInterface(realm, iface, false)
if err != nil {
return err
}
Expand All @@ -373,7 +373,7 @@ func installInterface(realm string, iface interfaces.AstarteInterface) error {
}

func updateInterface(realm string, interfaceName string, interfaceMajor int, newInterface interfaces.AstarteInterface) error {
updateInterfaceCall, err := astarteAPIClient.UpdateInterface(realm, interfaceName, interfaceMajor, newInterface, true)
updateInterfaceCall, err := astarteAPIClient.UpdateInterface(realm, interfaceName, interfaceMajor, newInterface, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/spf13/cobra"
)

var version = "24.5.0-dev"
var version = "24.5.2"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
package config

import (
"gopkg.in/yaml.v2"
"os"
"path"

"gopkg.in/yaml.v2"
)

const baseConfigName = "astartectl"
Expand Down Expand Up @@ -58,5 +57,6 @@ func SaveBaseConfiguration(configDir string, configuration BaseConfigFile) error
if err != nil {
return err
}

return os.WriteFile(path.Join(configDir, baseConfigName+".yaml"), contents, 0644)
}
55 changes: 53 additions & 2 deletions config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config

import (
"errors"
"fmt"
"os"
"path"
"strings"
Expand Down Expand Up @@ -43,7 +44,13 @@ func ConfigureViper(contextOverride string) error {
}

// Now, get the current context
currentContext := viper.Get("context").(string)
currentContextInterface := viper.Get("context")
currentContext := ""
//in case of empty file, the following check used to fail, now it defaults to empty context and enable file rewrite
if currentContextInterface != nil {
currentContext = currentContextInterface.(string)
}

// Check overrides
if contextOverride != "" {
currentContext = contextOverride
Expand Down Expand Up @@ -118,7 +125,16 @@ func contextsDirFromConfigDir(configDir string) string {

func listYamlNames(dirName string) ([]string, error) {
file, err := os.Open(dirName)
if err != nil {

if os.IsNotExist(err) {
//if we cannot open the directory, create it and open again
_ = os.MkdirAll(dirName, 0755)
file, err = os.Open(dirName)
if err != nil {
return nil, err
}

} else if err != nil {
return nil, err
}
defer file.Close()
Expand Down Expand Up @@ -178,3 +194,38 @@ func ensureConfigDirectoryStructure(configDir string) error {
}
return nil
}

func GetBaseConfig(configDir string) BaseConfigFile {

baseConfig, err := LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
// Now set the current context to the new one
baseConfig.CurrentContext = ""

if err := SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
return baseConfig
}
func UpdateBaseConfigWithContext(configDir, context string) BaseConfigFile {
baseConfig, err := LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
// Now set the current context to the new one
}

baseConfig.CurrentContext = context

if err := SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return baseConfig
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5
github.com/Masterminds/semver/v3 v3.1.1
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/astarte-platform/astarte-go v0.92.1-0.20240405095359-2af3738f0d84
github.com/astarte-platform/astarte-go v0.92.1
github.com/go-openapi/strfmt v0.21.1 // indirect
github.com/google/go-cmp v0.5.8
github.com/google/go-github/v30 v30.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/astarte-platform/astarte-go v0.92.1-0.20240405095359-2af3738f0d84 h1:E6l76r5aVFdXe5y0Oe0WxriXFletTz+RPVTiZlci1aQ=
github.com/astarte-platform/astarte-go v0.92.1-0.20240405095359-2af3738f0d84/go.mod h1:JY2jLeZoUP9o8+IZSIR595FgdjBj1vdYv4KuJ+tmq1U=
github.com/astarte-platform/astarte-go v0.92.1 h1:9zUiw1E4uj2wBDeszxGh/IAnVFg7BDKhRWqioksG0WA=
github.com/astarte-platform/astarte-go v0.92.1/go.mod h1:JY2jLeZoUP9o8+IZSIR595FgdjBj1vdYv4KuJ+tmq1U=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down

0 comments on commit 0be8ab2

Please sign in to comment.