-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created commando create from device groups
- Loading branch information
Showing
9 changed files
with
239 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "Mob123ile", | ||
"user_agent": "Mobile|Android|iPhone" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"results": { | ||
"id": 2260, | ||
"name": "Mob123il22e", | ||
"user_agent": "Mobile|Android|iPhone" | ||
}, | ||
"schema_version": 3 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package create | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/MakeNowJust/heredoc" | ||
"os" | ||
|
||
msg "github.com/aziontech/azion-cli/messages/device_groups" | ||
api "github.com/aziontech/azion-cli/pkg/api/edge_applications" | ||
|
||
"github.com/aziontech/azion-cli/pkg/cmdutil" | ||
"github.com/aziontech/azion-cli/utils" | ||
sdk "github.com/aziontech/azionapi-go-sdk/edgeapplications" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Fields struct { | ||
ApplicationID int64 | ||
Name string | ||
UserAgent string | ||
Path string | ||
} | ||
|
||
func NewCmd(f *cmdutil.Factory) *cobra.Command { | ||
fields := &Fields{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: msg.DeviceGroupsUsage, | ||
Short: msg.DeviceGroupsShortDescription, | ||
Long: msg.DeviceGroupsLongDescription, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
Example: heredoc.Doc(` | ||
$ azioncli device_groups create --application-id 1673635839 --name "asdf" --user-agent "httpbin.org" | ||
$ azioncli device_groups create -a 1673635839 --name "asdf" --user-agent "httpbin.org" | ||
$ azioncli device_groups create -a 1673635839 --in "create.json" | ||
`), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
request := api.CreateDeviceGroupsRequest{} | ||
if cmd.Flags().Changed("in") { | ||
var ( | ||
file *os.File | ||
err error | ||
) | ||
if fields.Path == "-" { | ||
file = os.Stdin | ||
} else { | ||
file, err = os.Open(fields.Path) | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", utils.ErrorOpeningFile, fields.Path) | ||
} | ||
} | ||
err = cmdutil.UnmarshallJsonFromReader(file, &request) | ||
if err != nil { | ||
return utils.ErrorUnmarshalReader | ||
} | ||
} else { | ||
if !cmd.Flags().Changed("application-id") || !cmd.Flags().Changed("name") || | ||
!cmd.Flags().Changed("user-agent") { // flags requireds | ||
return msg.ErrorMandatoryCreateFlags | ||
} | ||
|
||
request.SetName(fields.Name) | ||
request.SetUserAgent(fields.UserAgent) | ||
} | ||
|
||
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) | ||
response, err := client.CreateDeviceGroups(context.Background(), &request, fields.ApplicationID) | ||
if err != nil { | ||
return fmt.Errorf(msg.ErrorCreateDeviceGroups.Error(), err) | ||
} | ||
fmt.Fprintf(f.IOStreams.Out, msg.DeviceGroupsCreateOutputSuccess, response.GetId()) | ||
return nil | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.Int64VarP(&fields.ApplicationID, "application-id", "a", 0, msg.DeviceGroupsCreateFlagEdgeApplicationId) | ||
flags.StringVar(&fields.Name, "name", "", msg.DeviceGroupsCreateFlagName) | ||
flags.StringVar(&fields.UserAgent, "user-agent", "", msg.DeviceGroupsCreateFlagUserAgent) | ||
flags.StringVar(&fields.Path, "in", "", msg.DeviceGroupsCreateFlagIn) | ||
flags.BoolP("help", "h", false, msg.DeviceGroupsFlagHelp) | ||
return cmd | ||
} | ||
|
||
func prepareAddresses(addrs []string) (addresses []sdk.CreateOriginsRequestAddresses) { | ||
var addr sdk.CreateOriginsRequestAddresses | ||
for _, v := range addrs { | ||
addr.Address = v | ||
addresses = append(addresses, addr) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package create | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
msg "github.com/aziontech/azion-cli/messages/device_groups" | ||
"github.com/aziontech/azion-cli/pkg/httpmock" | ||
"github.com/aziontech/azion-cli/pkg/testutils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
t.Run("create new device groups", func(t *testing.T) { | ||
mock := &httpmock.Registry{} | ||
|
||
mock.Register( | ||
httpmock.REST("POST", "edge_applications/1673635846/device_groups"), | ||
httpmock.JSONFromFile(".fixtures/response.json"), | ||
) | ||
|
||
f, stdout, _ := testutils.NewFactory(mock) | ||
cmd := NewCmd(f) | ||
cmd.SetArgs([]string{ | ||
"--application-id", "1673635846", | ||
"--name", "Mob123il22e", | ||
"--user-agent", "Mobile|Android|iPhone", | ||
}) | ||
|
||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
require.Equal(t, fmt.Sprintf(msg.DeviceGroupsCreateOutputSuccess, 2260), stdout.String()) | ||
}) | ||
|
||
t.Run("create with file", func(t *testing.T) { | ||
mock := &httpmock.Registry{} | ||
|
||
mock.Register( | ||
httpmock.REST("POST", "edge_applications/1673635846/device_groups"), | ||
httpmock.JSONFromFile(".fixtures/response.json"), | ||
) | ||
|
||
f, stdout, _ := testutils.NewFactory(mock) | ||
|
||
cmd := NewCmd(f) | ||
cmd.SetArgs([]string{ | ||
"--application-id", "1673635846", | ||
"--in", ".fixtures/request.json", | ||
}) | ||
|
||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
require.Equal(t, fmt.Sprintf(msg.DeviceGroupsCreateOutputSuccess, 2260), stdout.String()) | ||
}) | ||
|
||
t.Run("bad request status 400", func(t *testing.T) { | ||
mock := &httpmock.Registry{} | ||
|
||
mock.Register( | ||
httpmock.REST("POST", "edge_applications/1673635846/device_groups"), | ||
httpmock.StatusStringResponse(http.StatusBadRequest, "Invalid"), | ||
) | ||
|
||
f, _, _ := testutils.NewFactory(mock) | ||
|
||
cmd := NewCmd(f) | ||
err := cmd.Execute() | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("internal server error 500", func(t *testing.T) { | ||
mock := &httpmock.Registry{} | ||
|
||
mock.Register( | ||
httpmock.REST("POST", "edge_applications/1673635846/device_groups"), | ||
httpmock.StatusStringResponse(http.StatusInternalServerError, "Invalid"), | ||
) | ||
|
||
f, _, _ := testutils.NewFactory(mock) | ||
|
||
cmd := NewCmd(f) | ||
err := cmd.Execute() | ||
require.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters