Ver código fonte

GTM version DTO

wfansh 6 meses atrás
pai
commit
c54b4a053b

+ 28 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/dto/GTMVersionDTO.java

@@ -0,0 +1,28 @@
+package com.wechi.adweb.bridge.google.gtm.dto;
+
+import com.wechi.adweb.bridge.google.common.ResourceDTO;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.ToString;
+import lombok.experimental.SuperBuilder;
+
+/**
+ * @author wfansh
+ */
+@Data
+@SuperBuilder
+@ToString(callSuper = true)
+@EqualsAndHashCode(callSuper = true)
+public class GTMVersionDTO extends ResourceDTO {
+
+    private String accountId;
+
+    private String containerId;
+
+    private boolean deleted;
+
+    private String description;
+
+    private String fingerprint;
+}

+ 41 - 5
src/main/java/com/wechi/adweb/bridge/google/gtm/service/GTMService.java

@@ -12,10 +12,7 @@ import com.google.auth.http.HttpCredentialsAdapter;
 import com.google.auth.oauth2.GoogleCredentials;
 import com.google.common.annotations.VisibleForTesting;
 import com.wechi.adweb.bridge.exception.DataException;
-import com.wechi.adweb.bridge.google.gtm.dto.GTMAccountDTO;
-import com.wechi.adweb.bridge.google.gtm.dto.GTMContainerDTO;
-import com.wechi.adweb.bridge.google.gtm.dto.GTMTriggerDTO;
-import com.wechi.adweb.bridge.google.gtm.dto.GTMWorkspaceDTO;
+import com.wechi.adweb.bridge.google.gtm.dto.*;
 import com.wechi.adweb.bridge.util.JsonUtils;
 
 import lombok.extern.slf4j.Slf4j;
@@ -25,6 +22,8 @@ import org.springframework.stereotype.Service;
 
 import java.io.IOException;
 import java.security.GeneralSecurityException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.Collections;
 import java.util.List;
 
@@ -43,7 +42,7 @@ public class GTMService {
     @Value("${google.gtm.container.admins}")
     private List<String> containerAdmins;
 
-    private TagManager tagManager;
+    public TagManager tagManager;
 
     @PostConstruct
     private void init() throws GeneralSecurityException, IOException {
@@ -307,4 +306,41 @@ public class GTMService {
             throw new DataException(e);
         }
     }
+
+    @VisibleForTesting
+    GTMVersionDTO createContainerVersion(String workspaceResourceName) throws DataException {
+        try {
+            CreateContainerVersionRequestVersionOptions versionOptions =
+                    new CreateContainerVersionRequestVersionOptions();
+            versionOptions.setName(
+                    "Latest version #"
+                            + LocalDateTime.now()
+                                    .format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
+
+            ContainerVersion createdContainerVersion =
+                    tagManager
+                            .accounts()
+                            .containers()
+                            .workspaces()
+                            .createVersion(workspaceResourceName, versionOptions)
+                            .execute()
+                            .getContainerVersion();
+            log.info("Created container version {}", JsonUtils.toJson(createdContainerVersion));
+
+            // Converts and returns.
+            return GTMVersionDTO.builder()
+                    .id(createdContainerVersion.getContainerVersionId())
+                    .resourceName(createdContainerVersion.getPath())
+                    .displayName(createdContainerVersion.getName())
+                    .accountId(createdContainerVersion.getAccountId())
+                    .containerId(createdContainerVersion.getContainerId())
+                    .deleted(createdContainerVersion.getDeleted())
+                    .description(createdContainerVersion.getDescription())
+                    .fingerprint(createdContainerVersion.getFingerprint())
+                    .build();
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
 }

+ 0 - 4
src/main/java/com/wechi/adweb/bridge/util/DateUtils.java

@@ -8,10 +8,6 @@ import java.time.format.DateTimeFormatter;
  */
 public class DateUtils {
 
-    public static String format(LocalDate date, DateTimeFormatter targetFormat) {
-        return date.format(targetFormat);
-    }
-
     public static String format(
             String dateStr, DateTimeFormatter sourceFormat, DateTimeFormatter targetFormat) {
         LocalDate date = LocalDate.parse(dateStr, sourceFormat);