import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* @Author: li
* @Date: 2020/12/2/002 15:15
* @describe
*/
public class HarborUtil {
private final static String harborUsername = "admin";
private final static String harborPassword = "Aa123456";
private final static String harborUrl = "http://XXXXX:XXX/api/v2.0";
public static Map getUser() {
long userId = System.currentTimeMillis();
Map userMap = new HashMap();
userMap.put("username", "lma-" + userId);
userMap.put("password", userId + RandomString.generateString(new Random(), 10));
userMap.put("realname", "lma");
userMap.put("role_id", 2);
userMap.put("email", userId + "@zhuojianchina.com");
RestTemplate template = new RestTemplate();
HttpHeaders headers = createHeaders(harborUsername, harborPassword);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");
HttpEntity<String> entity = new HttpEntity<>(JSONObject.toJSONString(userMap), headers);
ResponseEntity<String> response = template.postForEntity(harborUrl + "/users", entity, String.class);
if (response.getStatusCode().is2xxSuccessful()) {
return userMap;
}
return null;
}
public static void deleteUser(String userId) {
RestTemplate template = new RestTemplate();
HttpHeaders headers = createHeaders(harborUsername, harborPassword);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");
HttpEntity<String> entity = new HttpEntity<>(headers);
template.exchange(harborUrl + "/users/" + userId, HttpMethod.DELETE, entity, String.class);
}
public static String getUserId(Map user) {
RestTemplate template = new RestTemplate();
HttpHeaders headers = createHeaders(user.get("username").toString(), user.get("password").toString());
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");
HttpEntity<String> entity = new HttpEntity<>(headers);
HttpEntity<String> response = template.exchange(harborUrl + "/users/current", HttpMethod.GET, entity, String.class);
return JSONObject.parseObject(response.getBody()).getString("user_id");
}
public static HttpHeaders createHeaders(String username, String password) {
return new HttpHeaders() {
{
String auth = username + ":" + password;
String authHeader = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes());
set("Authorization", authHeader);
}
};
}
public static void main(String[] args) {
Map user = getUser();
String user_id = getUserId(user);
deleteUser(user_id);
}
}