github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/access_policies.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package endpoints
    19  
    20  import (
    21  	"github.com/gin-gonic/gin"
    22  
    23  	"github.com/mysteriumnetwork/node/requests"
    24  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    25  )
    26  
    27  // swagger:model AccessPolicies
    28  type accessPolicyCollection struct {
    29  	Entries []accessPolicy `json:"entries"`
    30  }
    31  
    32  type accessPolicy struct {
    33  	ID          string       `json:"id"`
    34  	Title       string       `json:"title"`
    35  	Description string       `json:"description"`
    36  	Allow       []accessRule `json:"allow"`
    37  }
    38  
    39  type accessRule struct {
    40  	Type  string `json:"type"`
    41  	Value string `json:"value"`
    42  }
    43  
    44  type accessPoliciesEndpoint struct {
    45  	httpClient              *requests.HTTPClient
    46  	accessPolicyEndpointURL string
    47  }
    48  
    49  // NewAccessPoliciesEndpoint creates and returns access policies endpoint
    50  func NewAccessPoliciesEndpoint(httpClient *requests.HTTPClient, accessPolicyEndpointURL string) *accessPoliciesEndpoint {
    51  	return &accessPoliciesEndpoint{
    52  		httpClient:              httpClient,
    53  		accessPolicyEndpointURL: accessPolicyEndpointURL,
    54  	}
    55  }
    56  
    57  // swagger:operation GET /access-policies AccessPolicies
    58  //
    59  //	---
    60  //	summary: Returns access policies
    61  //	description: Returns list of access policies
    62  //	responses:
    63  //	  200:
    64  //	    description: List of access policies
    65  //	    schema:
    66  //	      "$ref": "#/definitions/AccessPolicies"
    67  //	  400:
    68  //	    description: Failed to parse or request validation failed
    69  //	    schema:
    70  //	      "$ref": "#/definitions/APIError"
    71  //	  500:
    72  //	    description: Internal server error
    73  //	    schema:
    74  //	      "$ref": "#/definitions/APIError"
    75  func (ape *accessPoliciesEndpoint) List(c *gin.Context) {
    76  	req, err := requests.NewGetRequest(ape.accessPolicyEndpointURL, "", nil)
    77  	if err != nil {
    78  		c.Error(err)
    79  		return
    80  	}
    81  	r := accessPolicyCollection{}
    82  	err = ape.httpClient.DoRequestAndParseResponse(req, &r)
    83  	if err != nil {
    84  		c.Error(err)
    85  		return
    86  	}
    87  
    88  	utils.WriteAsJSON(r, c.Writer)
    89  }
    90  
    91  // AddRoutesForAccessPolicies attaches access policies endpoints to router
    92  func AddRoutesForAccessPolicies(
    93  	httpClient *requests.HTTPClient,
    94  	accessPolicyEndpointURL string,
    95  ) func(*gin.Engine) error {
    96  	ape := NewAccessPoliciesEndpoint(httpClient, accessPolicyEndpointURL)
    97  	return func(g *gin.Engine) error {
    98  		g.GET("/access-policies", ape.List)
    99  		return nil
   100  	}
   101  
   102  }