Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

## [3.0.0] - 2024-04-16 #28

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.0.0] - 2024-04-16
- dashboard-api-go supports now v1.44.1 of Meraki Dashboard API.
- User-agent is now required. Format of string "ApplicationName VendorName".

## [2.0.9] - 2024-03-14
- New main branch SDK doesn't use the intended user agent string format #21
- Incorrect links to documentation in code #23 [Removed]
Expand Down Expand Up @@ -1338,4 +1342,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[2.0.7]: https://github.com/meraki/dashboard-api-go/compare/v2.0.6...2.0.7
[2.0.8]: https://github.com/meraki/dashboard-api-go/compare/v2.0.7...2.0.8
[2.0.9]: https://github.com/meraki/dashboard-api-go/compare/v2.0.8...2.0.9
[Unreleased]: https://github.com/meraki/dashboard-api-go/compare/v2.0.9...main
[3.0.0]: https://github.com/meraki/dashboard-api-go/compare/v2.0.9...3.0.0
[Unreleased]: https://github.com/meraki/dashboard-api-go/compare/v3.0.0...main
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ The client could be generated with the following parameters:
- `baseURL`: The base URL, FQDN or IP, of the MERAKI instance.
- `dashboardApiKey`: The meraki_key for access to API.
- `debug`: Boolean to enable debugging
- `sslVerify`: Boolean to enable or disable SSL certificate verification.
- `userAgent`: String, set the User-Agent Format (AplicationName VendorName).

```go
client, err = meraki.NewClientWithOptions("https://api.meraki.com/",
"MERAKI_KEY",
"true")
"MerakiKey",
"true", "AplicationName VendorName")
if err != nil {
fmt.Println(err)
return
}
nResponse, _, err := client.Administered.GetAdministeredIDentitiesMe()
if err != nil {
fmt.Println(err)
Expand All @@ -48,7 +52,7 @@ The client can be configured with the following environment variables:
- `MERAKI_BASE_URL`: The base URL, FQDN or IP, of the MERAKI instance.
- `MERAKI_DASHBOARD_API_KEY`: The meraki_key for access to API.
- `MERAKI_DEBUG`: Boolean to enable debugging
- `MERAKI_SSL_VERIFY`: Boolean to enable or disable SSL certificate verification.
- `MERAKI_USER_AGENT`: String, set the User-Agent Format (AplicationName VendorName).

```go
Client, err = meraki.NewClient()
Expand All @@ -61,8 +65,8 @@ Here is an example of how we can generate a client, get a device count and then

```go
client, err = meraki.NewClientWithOptions("https://api.meraki.com/",
"Meraki_key",
"true")
"MerakiKey",
"true", "AplicationName VendorName")
if err != nil {
fmt.Println(err)
return
Expand All @@ -89,6 +93,7 @@ client, err = meraki.NewClientWithOptions("https://api.meraki.com/",
| SDK versions | MERAKI Dashboard version supported |
|--------------|------------------------------------|
| 2.y.z | 1.33.0 |
| 3.y.z | 1.44.1 |


## Changelog
Expand Down
13 changes: 2 additions & 11 deletions examples/administered/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

meraki "github.com/meraki/dashboard-api-go/v2/sdk"
meraki "github.com/meraki/dashboard-api-go/v3/sdk"
)

// Client is DNA Center API client
Expand All @@ -14,7 +14,7 @@ func main() {
fmt.Println("Authenticating")
client, err = meraki.NewClientWithOptions("https://api.meraki.com/",
"12f2eb53588c75e28d89e108a05ea0c2487b08cf",
"true")
"true", "AplicationName VendorName")
if err != nil {
fmt.Println(err)
return
Expand All @@ -30,14 +30,5 @@ func main() {
return
}

nResponse2, _, err := client.Administered.GetAdministeredIDentitiesMe()
if err != nil {
fmt.Println(err)
return
}
if nResponse2 != nil {
fmt.Println(nResponse)
return
}
fmt.Println("There's no data on response")
}
4 changes: 2 additions & 2 deletions examples/networks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

meraki "github.com/meraki/dashboard-api-go/v2/sdk"
meraki "github.com/meraki/dashboard-api-go/v3/sdk"
)

var client *meraki.Client
Expand All @@ -13,7 +13,7 @@ func main() {
fmt.Println("Authenticating")
client, err = meraki.NewClientWithOptions("https://api.meraki.com/",
"12f2eb53588c75e28d89e108a05ea0c2487b08cf",
"true")
"true", "AplicationName VendorName")
if err != nil {
fmt.Println(err)
return
Expand Down
6 changes: 3 additions & 3 deletions examples/organizations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

meraki "github.com/meraki/dashboard-api-go/v2/sdk"
meraki "github.com/meraki/dashboard-api-go/v3/sdk"
)

var client *meraki.Client
Expand All @@ -13,13 +13,13 @@ func main() {
fmt.Println("Authenticating")
client, err = meraki.NewClientWithOptions("https://api.meraki.com/",
"12f2eb53588c75e28d89e108a05ea0c2487b08cf",
"true")
"true", "AplicationName VendorName")
if err != nil {
fmt.Println(err)
return
}

nResponse, _, err := client.Organizations.GetOrganizations()
nResponse, _, err := client.Organizations.GetOrganizations(nil)
if err != nil {
fmt.Println(err)
return
Expand Down
4 changes: 2 additions & 2 deletions examples/testPagination/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"reflect"
"strings"

meraki "github.com/meraki/dashboard-api-go/v2/sdk"
meraki "github.com/meraki/dashboard-api-go/v3/sdk"
)

var client *meraki.Client
Expand All @@ -22,7 +22,7 @@ func main() {
fmt.Println("Authenticating")
client, err = meraki.NewClientWithOptions("https://api.meraki.com/",
"12f2eb53588c75e28d89e108a05ea0c2487b08cf",
"false")
"true", "AplicationName VendorName")
if err != nil {
fmt.Println(err)
return
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module github.com/meraki/dashboard-api-go/v2
module github.com/meraki/dashboard-api-go/v3

go 1.21.5

require (
github.com/go-resty/resty/v2 v2.11.0
github.com/google/go-querystring v1.1.0
github.com/juju/ratelimit v1.0.2
github.com/stretchr/testify v1.9.0
gopkg.in/h2non/gock.v1 v1.1.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/juju/ratelimit v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/net v0.17.0 // indirect
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslC
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -58,6 +59,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
Expand Down
7 changes: 6 additions & 1 deletion sdk/administered.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package meraki

import (
"fmt"
"net/http"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -42,6 +43,7 @@ type ResponseAdministeredGetAdministeredIDentitiesMeAuthenticationTwoFactor stru
func (s *AdministeredService) GetAdministeredIDentitiesMe() (*ResponseAdministeredGetAdministeredIDentitiesMe, *resty.Response, error) {
path := "/api/v1/administered/identities/me"
s.rateLimiterBucket.Wait(1)

response, err := s.client.R().
SetHeader("Content-Type", "application/json").
SetHeader("Accept", "application/json").
Expand All @@ -55,7 +57,10 @@ func (s *AdministeredService) GetAdministeredIDentitiesMe() (*ResponseAdminister
}

if response.IsError() {
return nil, response, fmt.Errorf("error with operation GetAdministeredIdentitiesMe")
if response.StatusCode() == http.StatusUnauthorized {
return s.GetAdministeredIDentitiesMe()
}
return nil, response, fmt.Errorf("error with operation GetApplications")
}

result := response.Result().(*ResponseAdministeredGetAdministeredIDentitiesMe)
Expand Down
39 changes: 29 additions & 10 deletions sdk/api_client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package meraki

import (
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strconv"
"time"

Expand All @@ -13,10 +15,11 @@ import (

const (
MERAKI_BASE_URL = "MERAKI_BASE_URL"
MERAKI_USER_AGENT = "MERAKI_USER_AGENT"
MERAKI_DASHBOARD_API_KEY = "MERAKI_DASHBOARD_API_KEY"
MERAKI_DEBUG = "MERAKI_DEBUG"
MERAKI_REQUESTS_PER_SECOND = "MERAKI_REQUESTS_PER_SECOND"
DEFAULT_USER_AGENT = "go-meraki/1.33.0"
DEFAULT_USER_AGENT = "go-meraki/1.44.1"
DEFAULT_REQUESTS_PER_SECOND = 10
)

Expand Down Expand Up @@ -53,7 +56,8 @@ func (c *Client) SetAuthToken(accessToken string) {
}

// SetUserAgent sets the User-Agent header in the request
func (c *Client) SetUserAgent(userAgent string) {
func (c *Client) SetUserAgent() {
userAgent := fmt.Sprintf("%s %s", DEFAULT_USER_AGENT, os.Getenv("MERAKI_USER_AGENT"))
c.common.client.SetHeader("User-Agent", userAgent)
}

Expand Down Expand Up @@ -97,7 +101,7 @@ func NewClient() (*Client, error) {
c.SetRequestsPerSecond(DEFAULT_REQUESTS_PER_SECOND)
}

c.SetUserAgent(DEFAULT_USER_AGENT)
c.SetUserAgent()
client.SetLogger(&CustomLogger{})
client.SetRetryCount(2)
client.SetRetryWaitTime(time.Second)
Expand Down Expand Up @@ -137,8 +141,8 @@ func NewClient() (*Client, error) {
}

// NewClientWithOptions creates a new API client with options passed with parameters
func NewClientWithOptions(baseURL string, dashboardApiKey string, debug string) (*Client, error) {
err := SetOptions(baseURL, dashboardApiKey, debug)
func NewClientWithOptions(baseURL string, dashboardApiKey string, debug string, userAgent string) (*Client, error) {
err := SetOptions(baseURL, dashboardApiKey, debug, userAgent)
if err != nil {
return nil, err
}
Expand All @@ -147,8 +151,8 @@ func NewClientWithOptions(baseURL string, dashboardApiKey string, debug string)
}

// NewClientWithOptionsAndRequests creates a new API client with options passed with parameters including the requests per second
func NewClientWithOptionsAndRequests(baseURL string, dashboardApiKey string, debug string, requestsPerSecond int) (*Client, error) {
err := SetOptionsWithRequests(baseURL, dashboardApiKey, debug, requestsPerSecond)
func NewClientWithOptionsAndRequests(baseURL string, dashboardApiKey string, debug string, userAgent string, requestsPerSecond int) (*Client, error) {
err := SetOptionsWithRequests(baseURL, dashboardApiKey, debug, userAgent, requestsPerSecond)
if err != nil {
return nil, err
}
Expand All @@ -157,8 +161,18 @@ func NewClientWithOptionsAndRequests(baseURL string, dashboardApiKey string, deb
}

// SetOptions sets the required environment variables
func SetOptions(baseURL string, dashboardApiKey string, debug string) error {
func SetOptions(baseURL string, dashboardApiKey string, debug string, userAgent string) error {
var err error

if !validateUserAgent(userAgent) {
return errors.New("user-agent bad format, expected: `AplicationName VendorName`")
}

err = os.Setenv(MERAKI_USER_AGENT, userAgent)
if err != nil {
return err
}

err = os.Setenv(MERAKI_BASE_URL, baseURL)
if err != nil {
return err
Expand All @@ -175,8 +189,8 @@ func SetOptions(baseURL string, dashboardApiKey string, debug string) error {
}

// SetOptionsWithRequests sets the required environment variables including the requests per second
func SetOptionsWithRequests(baseURL string, dashboardApiKey string, debug string, requestsPerSecond int) error {
err := SetOptions(baseURL, dashboardApiKey, debug)
func SetOptionsWithRequests(baseURL string, dashboardApiKey string, debug string, userAgent string, requestsPerSecond int) error {
err := SetOptions(baseURL, dashboardApiKey, debug, userAgent)
if err != nil {
return err
}
Expand Down Expand Up @@ -287,3 +301,8 @@ func convertToString(i interface{}) string {
return ""
}
}

func validateUserAgent(ua string) bool {
regex := regexp.MustCompile(`^\S+\s\S+$`)
return regex.MatchString(ua)
}
Loading
Loading