In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces how to use Swagger to call Harbor Registry's REST API, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Introduction to Swagger
Swagger is the most popular open source tool for RESTful API, which contains a complete set of code bases, editors, code generators, etc., which can be used for the description, definition, generation and visualization of API. We can view its detailed description at http://www.swagger.io, download its source code and integrate it into the project. Harbor is VMware's newly open source enterprise container Registry project (http://github.com/vmware/harbor)). Users can deploy Harbor in a private environment to achieve permissions management, graphical management, LDAP/AD authentication integration and log audit of container images. Harbor also provides RESTful API, and other container management platforms can easily integrate the functions of Harbor. This article describes how to use the Swagger tool embedded in Harbor to invoke and test RESTful API.
First, let's take a look at how Swagger describes and defines RESTful API. Swagger provides an online WYSIWYG editor (http://editor.swagger.io/). Users can enter YAML or JSON configurations in line with the Swagger specification on the left side of the editor, and the right side will show the actual effect in real time according to the input. If there is an error in the input, there will be tips to teach you how to correct it. It's really convenient! Please refer to (http://swagger.io/specification/) how to write a Swagger definition file that conforms to the specification.
This editor also supports downloading edited YAML files locally, or converting them to JSON format, and even helps us automatically generate test servers (Mock Server) or clients, and there are many features we can try.
The purpose of using Swagger is nothing more than two things: the separation of front and rear ends and testing according to the contract. The so-called front and back end separation means that the front and rear ends have their own development processes, construction tools, testing, etc., which are decoupled through RESTfulAPI to make the structure clear and concerns separated. Testing according to the contract means that the front and back end developers agree according to the request path, parameters and type of the release service to form a contract, which may be in JSON or YAML format. In the actual development process, the formation of the contract is a process of continuous improvement, which will certainly be revised and supplemented many times. Swagger precisely meets such an ever-changing and perfect demand, realizing the separation of front and rear, finding differences and making adjustments as early as possible in contract testing, so as to minimize the risk of final integration.
Swagger function embedded in Harbor
The core function of Harbor is also realized by RESTful API. In the development process, a set of visual API specification is written by Swagger and provided to users as part of the project.
The Harbor project uses two ways for users to use Swagger to present or manipulate RESTful API.
One is the "static way", which only uses Swagger as the presentation and lookup tool of Harbor RESTful API. Users only need to find the swagger.yaml file from the docs/ directory of the Harbor project, open it with an editor, select all, copy and paste it into the left code area of the Swagger online editor, and a visual Harbor RESTful API document page will be displayed on the right side, which is easy to consult and refer to.
The other is "dynamic", where Swagger UI and Harbor REST services are deployed in the same Server, and users can use Swagger to manipulate and test the RESTful API of Harbor. This method may modify the data in the database, so it is not recommended in production systems. The deployment scenario is shown in the following figure:
In the docs/ directory of the source code of the Harbor project, there is a script file for prepare-swagger.sh that can help users achieve "dynamic" deployment. The following is a brief description of the relevant steps. For more information, please see the document docs/configure_swagger.md:
(1) modify the SERVER_ IP value in the script file, set it to the host IP address of the current deployment Harbor system, save the changes, and execute the script. The script will help users download Swagger software in turn, extract it to the Harbor project vendors static resource directory, copy the swagger.yaml file under the docs/ directory to the Harbor project resources/yaml static resource directory, and modify the URL content according to the SERVER_IP replacement provided by the user.
(2) change to the Deploy directory, modify the docker-compose.yml file, and mount the newly added Swagger static resource directory to the Dockercontainer of HarborUI by volumes, so that SwaggerUI can be released to the outside world for access as HarborUI starts.
(3) rebuild the Harbor project with the docker-compose command, clean up the previously left container contents, and restart the newly built Harbor project image.
The following is a screenshot of the deployed Swagger UI page.
RESTful API authentication problem
You also need to pay attention to the "login status" issue when triggering Harbor RESTful API through Swagger UI, because some API requires session information. There are two ways to configure.
Method 1: first, open the UI interface through the browser (note: make sure that the IP address in the URL of Harbor UI is the same as the SERVER_IP value provided by the previously deployed Swagger UI), complete registration (first use), log in, and then open a new tab page in the same browser and enter the following Swagger UI address, so as to ensure that the HarborRESTful API can be manipulated when the user is logged in:
Http:///static/vendors/swagger/index.html
Method 2: Harbor RESTful API itself implements Basic Authentication authentication mode, but at present Swagger does not support inputting user name and password from the interface, which makes access inconvenient. Interested students can refer to the link below (https://github.com/swagger-api/swagger-ui) and try to modify Swagger to achieve Basic Authentication mode access. Of course, users can also use the command
Curl-u
To access API so that you can debug API directly without logging in to HarborUI in advance.
On how to use Swagger to call Harbor Registry REST API to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.