Automatize a segurança através do Azion SDK

ir para a documentação de referência do Azion SDK

O Azion SDK para Go serve como um poderoso conjunto de ferramentas para desenvolvedores, projetado para simplificar a integração dos serviços de API da Azion em aplicações da linguagem de programação Go.

Esta ferramenta facilita a interação com as APIs da Azion, capacitando os desenvolvedores a gerenciar eficientemente os recursos de segurança por meio de aplicações programáveis.


O seguinte trecho de código representa como o Azion SDK pode ser usado para facilitar a interação entre aplicações Go e os serviços de API da Azion, demonstrado como se cria um edge firewall na plataforma através da ferramenta:

package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
sdk "github.com/aziontech/azionapi-go-sdk/edgefirewall"
)
type Response struct {
Results struct {
Name string `json:"name"`
ID int `json:"id"`
} `json:"results"`
}
// Definir a URL do firewall como uma constante
const (
firewallURL = "https://api.azionapi.net/"
)
// Estrutura Client para armazenar o cliente da API
type Client struct {
apiClient sdk.APIClient
}
func main() {
// Solicitar o token pessoal do usuário
fmt.Println("Hey, there! Welcome to Edge Firewall example")
fmt.Println("Please, provide your Personal Token: ")
var personalToken string
fmt.Scanf("%s", &personalToken)
// Manipular o firewall com o token fornecido
err := firewallHandler(personalToken)
if err != nil {
fmt.Println(err)
}
}
// firewallHandler lida com as operações do firewall
func firewallHandler(personalToken string) error {
// Request the firewall name from the user
fmt.Println("Please, provide the Edge Firewall name: ")
reader := bufio.NewReader(os.Stdin)
firewallName, err := reader.ReadString('\n')
if err != nil {
return err
}
firewallName = strings.Replace(firewallName, "\n", "", -1)
// Criar um novo cliente e firewall
var httpClient *http.Client
client := NewClient(httpClient, firewallURL, personalToken)
err = client.NewFirewall(&firewallName)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
// NewFirewall cria um novo firewall
func (c *Client) NewFirewall(firewallName *string) error {
// Create a new context
ctx := context.Background()
// Criar uma nova requisição de firewall
firewall := new(sdk.CreateEdgeFirewallRequest)
firewall.Name = firewallName
fmt.Println("\nCreating Edge Firewall....\n")
// Executar a requisição do firewall
req := c.apiClient.DefaultAPI.EdgeFirewallPost(ctx).CreateEdgeFirewallRequest(*firewall)
_, httpResp, err := req.Execute()
if err != nil {
fmt.Println(httpResp)
return err
}
// Ler e imprimir a resposta
bytes, err := io.ReadAll(httpResp.Body)
if err != nil {
return err
}
var response Response
err = json.Unmarshal(bytes, &response)
if err != nil {
fmt.Println(httpResp)
return err
}
fmt.Println("Edge Firewall succesfully created\n")
fmt.Println("Name: " + response.Results.Name)
fmt.Println("ID: " + strconv.Itoa(response.Results.ID) + "\n")
return nil
}
// NewClient cria um novo cliente com o cliente HTTP, URL e token fornecidos
func NewClient(httpClient *http.Client, url string, token string) *Client {
// Criar uma nova configuração
conf := sdk.NewConfiguration()
// Definir o cliente HTTP e os cabeçalhos
conf.HTTPClient = httpClient
conf.AddDefaultHeader("Authorization", "token "+token)
conf.AddDefaultHeader("Accept", "application/json;version=3")
conf.Servers = sdk.ServerConfigurations{
{URL: url},
}
// Retornar um novo cliente com a configuração
return &Client{
apiClient: *sdk.NewAPIClient(conf),
}
}



Contributors