Ver Fonte

Delete container and property

wfansh há 6 meses atrás
pai
commit
da141d3399

+ 24 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/controller/GAController.java

@@ -114,6 +114,30 @@ public class GAController extends BaseController {
                 .build();
     }
 
+    @PostMapping("/properties/delete")
+    @ResponseBody
+    public OpenAPIResponse<String> deleteProperty(@RequestBody OpenAPIRequest<String> apiRequest)
+            throws BadRequestException, DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** deleteProperty() ****** apiRequest = {}", JsonUtils.toJson(apiRequest));
+        String propertyResourceName = apiRequest.getData();
+
+        // 1. Validates the request parameters.
+        if (StringUtils.isEmpty(propertyResourceName)) {
+            throw new BadRequestException(apiRequest);
+        }
+
+        // 2. Executes the API request.
+        gaAdminService.deleteProperty(
+                // Converts to resource name if the account ID is provided by mistake.
+                GAPropertyDTO.toResourceName(propertyResourceName));
+        log.info("****** deleteProperty() ****** duration = {} seconds", getElapsedSeconds(start));
+        return OpenAPIResponse.<String>builder()
+                .status(APIStatus.SUCCESS)
+                .data(propertyResourceName)
+                .build();
+    }
+
     @PostMapping("/report")
     @ResponseBody
     public OpenAPIResponse<List<? extends GAReportDataDTO>> runReport(

+ 6 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/GAPropertyDTO.java

@@ -7,6 +7,8 @@ import lombok.EqualsAndHashCode;
 import lombok.ToString;
 import lombok.experimental.SuperBuilder;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.util.List;
 
 /**
@@ -37,4 +39,8 @@ public class GAPropertyDTO extends ResourceDTO {
     private long updateTime;
 
     private List<GADataStreamDTO> dataStreams;
+
+    public static String toResourceName(String id) {
+        return StringUtils.isNumeric(id) ? "properties" + RESOURCE_NAME_SPLITTER + id : id;
+    }
 }

+ 11 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/service/GAAdminService.java

@@ -236,4 +236,15 @@ public class GAAdminService {
             throw new DataException(e);
         }
     }
+
+    public void deleteProperty(String propertyResourceName) throws DataException {
+        try (AnalyticsAdminServiceClient analyticsAdminServiceClient =
+                AnalyticsAdminServiceClient.create(adminServiceSettings)) {
+            analyticsAdminServiceClient.deleteProperty(propertyResourceName);
+            log.info("Deleted property {}", propertyResourceName);
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
 }

+ 22 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/controller/GTMController.java

@@ -69,4 +69,26 @@ public class GTMController extends BaseController {
                 .data(gtmContainers)
                 .build();
     }
+
+    @PostMapping("/containers/delete")
+    @ResponseBody
+    public OpenAPIResponse<String> deleteContainer(@RequestBody OpenAPIRequest<String> apiRequest)
+            throws BadRequestException, DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** deleteContainer() ****** apiRequest = {}", JsonUtils.toJson(apiRequest));
+        String containerResourceName = apiRequest.getData();
+
+        // 1. Validates the request parameters.
+        if (StringUtils.isEmpty(containerResourceName)) {
+            throw new BadRequestException(apiRequest);
+        }
+
+        // 2. Executes the API request.
+        gtmService.deleteContainer(containerResourceName);
+        log.info("****** deleteContainer() ****** duration = {} seconds", getElapsedSeconds(start));
+        return OpenAPIResponse.<String>builder()
+                .status(APIStatus.SUCCESS)
+                .data(containerResourceName)
+                .build();
+    }
 }

+ 11 - 1
src/main/java/com/wechi/adweb/bridge/google/gtm/service/GTMService.java

@@ -162,6 +162,16 @@ public class GTMService {
         }
     }
 
+    public void deleteContainer(String containerResourceName) throws DataException {
+        try {
+            tagManager.accounts().containers().delete(containerResourceName).execute();
+            log.info("Deleted container {}", containerResourceName);
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
+
     @VisibleForTesting
     void initContainerAccess(String accountResourceName, String containerId) throws DataException {
         try {
@@ -348,7 +358,7 @@ public class GTMService {
     void publishContainerVersion(String versionResourceName) throws Exception {
         try {
             tagManager.accounts().containers().versions().publish(versionResourceName).execute();
-            log.info("Published container version {}", JsonUtils.toJson(versionResourceName));
+            log.info("Published container version {}", versionResourceName);
         } catch (IOException e) {
             log.error(e.getMessage());
             throw new DataException(e);